Advertisement
michaelyuen

Untitled

Oct 30th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. <?php
  2. $error = [];
  3. $requireds = ['field1','field2','field3'];
  4. if (ISSET($_POST['field1'],$_POST['field2'],$_POST['field3'])) {
  5.     // do something
  6.     foreach ($requireds as $required) {
  7.         if (array_key_exists($required, $_POST)) {
  8.             switch ($required) {
  9.                 case 'field1':
  10.                     if ($_POST[$required] != 'your condition') {
  11.                         $error[] = 'Validate field1 failed';
  12.                     } else {
  13.                         $field1 = $_POST[$field1];
  14.                     }
  15.                 break;
  16.                 case 'field2':
  17.                     if ($_POST[$required] != 'your condition') {
  18.                         $error[] = 'Validate field2 failed';
  19.                     } else {
  20.                         $field2 = $_POST[$field2];
  21.                     }
  22.                 break;
  23.                 default:
  24.                 break;
  25.             }
  26.         } else {
  27.             $error[] = 'Missing required field '. $required;
  28.         }
  29.     }
  30.    
  31.    
  32.    
  33.     if (count($error) > 0) {
  34.         // there are some errors;
  35.         // do something to handle your errors;
  36.     } else {
  37.         // validation pass;
  38.         // do what you want to do with $field1 and $field2;
  39.     }
  40. }
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement