Server Side Form Validation


/ Published in: PHP
Save to your folder(s)

A generic loop-based form validation to check which _POST value is empty.

Enter the form name in Line 3 and put the description in Line 4.


Copy this code and paste it in your HTML
  1. //---- validation ------ //
  2. $count = 0;
  3. $required_fields = array('name', 'id_no');
  4. $required_fields_description = array('Name of the user', 'Id');
  5.  
  6. //check all required forms
  7. foreach($required_fields as $fields) {
  8. if ($_POST[$fields] == "") $error_check .= "You need to enter field '".$required_fields_description[$count]."'. Go back. <br />";
  9. $count++;
  10. }
  11.  
  12. if ($error_check != "") die($error_check);
  13. //----- end validation ------//

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.