empty space validation in PHP with a for loop -


as can see, have simple registration form, , php script validate fields. idea here is, if have 20+ fields, , required at-least user fill .. username, lastname , age, store in array have done below.

$needed = array("username", "lastname", "age"); so, can see in code, for loop check if 1 of them filled, code works part. ex: if don't fill 3 fields

you must fill username continue

you must fill lastname continue

you must fill age continue

but, if fill on field , left other two, or fill 2 , leave one, echo '<p>required fileds filled</p>';

so, problem here that, fields should filled before script can go saying echo '<p>required fileds filled</p>';

<pre> <form action='' method='post'>     <input type='text' name='username' />     <input type='text' name='lastname' />     <input type='text' name='age' />     <input type='text' name='gender' />     <input type='text' name='country' />     <input type='submit' name='reqirester' /> </form>   <?php       $needed = array("username", "lastname", "age");    if($_post):     $check = null; for($i=0; $i < count($needed); $i++){          if($_post[$needed[$i]] == ''){           echo '<p>you must fill '.$needed[$i].' continue<p/>';         break;            }else {             echo '<p>required fileds filled</p>';         }     }  endif; 

why not:

$required = array("username", "lastname", "age"); $missing = array_keys(array_diff_key(array_flip($required), array_filter($_post)));  if($missing)   printf('you missed: %s', implode(', ', $missing)); 

or using output:

foreach($missing $key)   printf('<p>you must fill %s continue</p>', $key);  if(!$missing)   print '<p>required fileds filled</p>'; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -