Post Validation Loop


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



Copy this code and paste it in your HTML
  1. $requiredFields = array('field1', 'field2', 'field3', 'field4');
  2. $formData = getFormData($requiredFields);
  3.  
  4. function getFormData($requiredFields){
  5. $formData = array();
  6. $formData['valid'] = true;
  7. $formData['fields'] = array();
  8. $formData['notValidFields'] = array();
  9.  
  10. for($a = 0; $a < count($requiredFields); $a++){
  11. $field = $requiredFields[$a];
  12. if(isset($_POST[$field])){
  13. $value = $_POST[$field];
  14. if(empty($value)){
  15. $formData['valid'] = false;
  16. $formData['notValidFields'][] = $field;
  17. }else{
  18. $formData['fields'][$field] = $value;
  19. }
  20. }else{
  21. $formData['valid'] = false;
  22. $formData['notValidFields'][] = $field;
  23. }
  24. }
  25. return $formData;
  26. }

URL: http://css-tricks.com/snippets/php/post-validation-loop/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.