Jump to content

Form Validation


robmo

Recommended Posts

Hi,

 

I am trying to setup a PHP script that will validate an html form. The form and the PHP script are separate from each other. The code that I have was modified from a tutorial on YouTube. The example used there works but as I have began to tailor it to my purpose, it is now broke. I just added one line for validation to simplify troubleshooting. If I find an empty field on the form, I would like to display the error message on the form itself rather than start a new html page with the PHP script. I am not sure if that is possible.

 

Here is my form:

http://www.tallfirshoa.com/adform.htm

 

YouTube videos that I was working from. The video's example has the form and PHP combined as one. In my case, I need the form and PHP separate.

http://www.youtube.com/watch?v=yuLpSospbBk&feature=search

http://www.youtube.com/watch?v=LF5zTWthpn0&feature=search

 

The code works fine if I leave out the validation. As soon as I add if(!$fname), the script becomes broken. I'm guessing that it has something to do with $errorstring and how it works. I am new to PHP and have tried as many things as I can think of but I can't make any forward progress.

 

Thanks for your help!

 

Rob

 

<html>

<h1>Tall Firs Ad Submission</h1>

<?php
if ($_POST['submit'])
{
//Get form data
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$displayemail = $_POST['displayemail'];
$phone = $_POST['phone'];
$category = $_POST['category'];
$description = $_POST['description'];

//Declare variables
$to = "email@gmail.com";
$subject = "Request";
$headers = "From: $email \r\n
            Reply-To: $email";
}
//Setup form validation

$errorstring = ""  //default value of error string

//Begin validation
if (!$fname)
    $errorstring = $errorstring."First Name<br>";


?>


</html>

 

 

Link to comment
Share on other sites

First off, you need to move the if(!fname) into the if($_POST['submit']).

if ($_POST['submit'])
{
//Get form data
   $fname = $_POST['fname'];
   $lname = $_POST['lname'];
   $email = $_POST['email'];
   $displayemail = $_POST['displayemail'];
   $phone = $_POST['phone'];
   $category = $_POST['category'];
   $description = $_POST['description'];

   //Begin validation
   if (!$fname){
      $errorstring = $errorstring."First Name<br>";
   }
   
//Declare variables
   $to = "email@gmail.com";
   $subject = "Request";
   $headers = "From: $email \r\n
               Reply-To: $email";

}
//Setup form validation

$errorstring = ""  //default value of error string

 

The reason it wasn't working was because you were not declaring $fname anyhwere except in the $_POST.

Link to comment
Share on other sites

I can't believe I did that. I guess you go crazy and miss simple things when you look at it too hard and too long.

 

I added some additional code and it does tell you if you need to fill out something. For now it's setup so the PHP script generates a new html page and displays which fields require information. From there, users would have to hit the back button to correct the fields called out.

 

Is it possible to display the error messages on the form itself rather than generating a new page? I'd basically like the Submit to fail and not change the page at all yet display the error messages where appropriate.

 

I like your idea with the array. There are a number of variables and it could really reduce lines of code by looping through them.

Link to comment
Share on other sites

You could do something like this:

if($_POST['submit']{
   //get your form data
   foreach($_POST as $key=>$value){
      $$key = $value;
   }

   //set a default
   $errors = false;

   //form validate
   if(!fname){
      $errors = true;
      $errormessage = "Your message";
   }

   //if no errors, send information
   if($errors == false){
      //Information things
   }
}

 

Then, somewhere above your form you put

echo $errormessage;

Link to comment
Share on other sites

First off, you need to move the if(!fname) into the if($_POST['submit']).

 

The reason it wasn't working was because you were not declaring $fname anyhwere except in the $_POST.

 

hcdarkmage,

 

I'm not sure I understand what you're saying here. Are you saying you have to declare the variables in advance to using them? Like using DIM in visual basic?

 

When I added the missing semicolon after the $errorstring statement, the script started working so I'm not sure how your explanation helps me.

 

Do you think you could clarify your meaning?

 

Thanks a lot!

Rob

Link to comment
Share on other sites

First off, you need to move the if(!fname) into the if($_POST['submit']).

 

The reason it wasn't working was because you were not declaring $fname anyhwere except in the $_POST.

 

hcdarkmage,

 

I'm not sure I understand what you're saying here. Are you saying you have to declare the variables in advance to using them? Like using DIM in visual basic?

 

When I added the missing semicolon after the $errorstring statement, the script started working so I'm not sure how your explanation helps me.

 

Do you think you could clarify your meaning?

 

Thanks a lot!

Rob

 

No what he is saying is, if $_POST['submit'] was not set, you would always get the $errorstring for fname.  Moving it inside of the $_POST if statement would only return the error string, IF the form had been posted.

Link to comment
Share on other sites

jcbones,

 

Ok, that makes sense now. Unless Submit was clicked though the PHP script would never be called so does it really matter? It sounds like the way I have it is not best practice even if does happen to work in my example. I will move the code as suggested. Should I also move the $errorstring = ""; into that area as well?

 

Thanks again,

Rob

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.