Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('METHOD')? null : define('METHOD',$_SERVER['REQUEST_METHOD']);
- function requiredInputs(){
- $a = func_get_args();
- if(is_array($a[0])){ $a = $a[0];}
- $c = func_num_args();
- $r = Array('errors'=>false);
- for($i=0;$i<$c;++$i){
- if(!isset($_POST[$a[$i]])){
- $r['missing'][] = $a[$i];
- $r['errors'] = true;
- }
- }
- return $r;
- }
- $items = Array('item1','item2','item3','item4','item5');
- if(METHOD === 'POST'){
- echo '<pre>';
- $check = requiredInputs('item1','item2','item3','item4','item5'); // Or requiredInputs($items); It can take a single array or a list of fields
- if($check['errors'] === true){
- echo 'The following were missing from the form input:' . PHP_EOL;
- echo implode(PHP_EOL, $check['missing']) . PHP_EOL;
- }else{
- echo 'All Form Inputs were received.' . PHP_EOL;
- }
- echo PHP_EOL;
- echo '$_POST = ';
- print_r($_POST);
- exit;
- }else{
- // This just creates some random form fields to test.
- $form = '';
- foreach($items as $item){
- if(rand(0,10) >= 5){
- $value = (rand(0,10) >= 5)? 'someValue' : '';
- $form .= '<input type="text" name="' . $item . '" value="' . $value . '"><br>' . PHP_EOL;
- }else{
- $form .= '<span>Missing: ' . $item . '</span><br>' . PHP_EOL;
- }
- }
- }
- ?>
- <html>
- <head>
- </head>
- <body>
- <form method="post">
- <?php echo $form; ?>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement