Form validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Form validation

Post by kkonline »

Below is the extract of a validation script

If no title is typed then it gives the desired message "fill your title properly." but for content validation even if no content is submitted it does not echo the message but continues to the next part of the script

Code: Select all

//validation for next field

	      if(isset($_POST['title']))//title field is set
			  {
			   $n = $_POST['title'];
			   if (strlen($n) > 0 && strlen($n) < 61) //valid and sql friendly name now in $name
			     {
	$title = trim(mysql_real_escape_string($_POST['title']));
                       }
			   else {
			     // $n is not valid
                     echo "fill your title properly.";
$fault++;
				exit;
			        }
                   } 	
			else {
				//name not set
			   echo "you left the title field blank.";
$fault++;	
			exit;
			     } 	

//validation for next field
	      if(isset($_POST['content']))//content  field is set
			  {
			   	$content = trim(mysql_real_escape_string($_POST['content']));

                   } 	
			else {
				//name not set
			   echo "left the content field blank.";
$fault++;
				exit;
			     }
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post by Defkon1 »

i think that's because post variable from html forms are always created (except in case of checkboxes)... so isset() will be always TRUE, but with empty value...
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

Defkon1 wrote:i think that's because post variable from html forms are always created (except in case of checkboxes)... so isset() will be always TRUE, but with empty value...
That's right.

Try this instead, set a variable when the form is submitted, lets call it $query.

Code: Select all

$query = '';
Then perform your tests so, for the first one

Code: Select all

if($_POST['title'] == ''){
     $query .= 'you left the title field blank. '; 
}
notice the space after the sentance. Also I am at a loss to remember if it should be $query .= or $query =. which is what happens when I don't sleep well.

Ok, so keep cycling through your validation rules until you get to the end and then you need a simple script to check if the form is valid:

Code: Select all

if($query != ''){
     // Put your actions here for an invalid form
}else{
     // Process the form
}
I tend to do it in both PHP and Javascript since you get a better level of user feedback with Javascript. It gives you the option of changing the colour of field names that are wrong and only having to submit the form once.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

The empty function is always rather useful too.

Code: Select all

if(empty($var))
{
      //SET ERROR
} else
      //VALIDATE VAR VALUE
}
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

another simple solution would be to use

Code: Select all

if(strlen($_POST["content"]) > 0)
instead of

Code: Select all

if(isset($_POST['content']))/php]
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

aceconcepts wrote:The empty function is always rather useful too.

Code: Select all

if(empty($var))
{
      //SET ERROR
} else { // (Edited for accuracy)
      //VALIDATE VAR VALUE
}
Yeah, that works too, rather well in fact. :wink:
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Thanks for the edit "{". I'm so blind 8)
Post Reply