PHP form validation - question

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
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

PHP form validation - question

Post by tawfiq »

Hi,

I am trying to acheive the followings, if you have any better idea I would like to hear from you. Basically, is there a way I can do this without having to print the form twice when there is an error? I would like to highlight areas so that users know where they are making mistakes. (i.e using red border around text field or just simply changing the colour of the labels. etc)

Code: Select all

 
<?
if(form is submitted){
 
$error='';
$error_name = '';
$error_age = '';
 
if(field1 is empty or field2 is empty){
 
$error.='fill in all required fileds'; 
 
}
 
// all fields have been filled with some data
else{
 
//check if field 1 holds the correct type i.e name and not digit
if(name holds anything other than character){
$error_name.= 'type only character pls';
}
 
//check if field 2 holds the correct type i.e number and not character
if(age holds anything other than number){
 
$error_age.= 'type only a number pls';
}
 
 
 
} 
 
//display form
}else{ 
 
?>
--- form starts  ----
 
name (formfield1)
age  (form field 2)
submit
 
--- form ends ----
 
<?
 } // end if 
 
if($error is not empty){
 
print $error
print entire form again
 
}
 
// point to the users where they have made mistakes (hightlight form fields etc)
else if($error_name is not empty || $error_age is not empty   ) {
 
print <form name action method>
 
if($error_name is empty){
 
print  name (formfield1)  // normal way
}
else{
print name (formfield1) // heightlight it using css , red coloured border  etc 
print error_name;
 
}
 
repeat the above steps for all other form fields
 
print </form>
 
}
 
?>
 
 
Strike
Forum Newbie
Posts: 3
Joined: Thu Mar 27, 2008 8:17 am
Location: Tabluha,Egypt.

Re: PHP form validation - question

Post by Strike »

Use javascript ..
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

Re: PHP form validation - question

Post by tawfiq »

I do have javascript error checking but javascript validation can be easily bypassed. So, you have to have server side validation in place.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: PHP form validation - question

Post by aceconcepts »

I would invoke the validation from within the same page.

This is how i would set up the validation:

Code: Select all

 
//error variables
$frmErr=0;
$fnErr=0;
 
if(isset(SUBMIT_BUTTON))
{
   $firstName=$_POST['firstName'];
 
   if(empty($firstName)) //example posted field
   {
     $frmErr=1; //set a single "form" error so that none of the data is processed
     $fnErr=1;   //set a field-specific error variable
   }
 
   //now check if there have been any errors - if not then process data
   if($frmErr==0)
   {
     //process data - inserts etc...
   }
}
 

Now, if there was an error the form will display as normal as you have all the validation within the same file.

To show error fields simply use an if statement in each one you want validated

e.g.

Code: Select all

 
<input name="firstName" type="text" value="<? echo $firstName; ?>" <? if($fnErr>0){ echo'class="errorField"'; } ?> /> //use some css to dress up the border etc...
 
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

Re: PHP form validation - question

Post by tawfiq »

Thank you, it makes perfect sense :D
Post Reply