Posted By

ddavidd on 03/11/11


Tagged


Versions (?)

Post Validation Loop


 / Published in: PHP
 

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

  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. }

Report this snippet  

You need to login to post a comment.