Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $error = [];
- $requireds = ['field1','field2','field3'];
- if (ISSET($_POST['field1'],$_POST['field2'],$_POST['field3'])) {
- // do something
- foreach ($requireds as $required) {
- if (array_key_exists($required, $_POST)) {
- switch ($required) {
- case 'field1':
- if ($_POST[$required] != 'your condition') {
- $error[] = 'Validate field1 failed';
- } else {
- $field1 = $_POST[$field1];
- }
- break;
- case 'field2':
- if ($_POST[$required] != 'your condition') {
- $error[] = 'Validate field2 failed';
- } else {
- $field2 = $_POST[$field2];
- }
- break;
- default:
- break;
- }
- }
- }
- if (count($error) > 0) {
- // there are some errors;
- // do something to handle your errors;
- } else {
- // validation pass;
- // do what you want to do with $field1 and $field2;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement