Page 1 of 1

PHP form validation - question

Posted: Thu Mar 27, 2008 7:07 am
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>
 
}
 
?>
 
 

Re: PHP form validation - question

Posted: Thu Mar 27, 2008 8:41 am
by Strike
Use javascript ..

Re: PHP form validation - question

Posted: Thu Mar 27, 2008 9:01 am
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.

Re: PHP form validation - question

Posted: Thu Mar 27, 2008 12:24 pm
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...
 

Re: PHP form validation - question

Posted: Fri Mar 28, 2008 4:36 am
by tawfiq
Thank you, it makes perfect sense :D