as you can see, I have a simple registration form, and a PHP script just to validate some fields. The idea here is, that if you have 20+ fields, and you required at-least the user to fill .. username, lastname and age, then you would store it in an array like I have done below.
$needed = array("username", "lastname", "age");
So, as you can see in the code, I do a for
loop to check if one of them is filled,
now the code works for the most part. for ex: if you don't fill the three fields it will say
You must fill username to continue
You must fill lastname to continue
You must fill age to continue
But, if You fill on field and left the other two, or fill two and then leave one, it will just echo '<p>Required fileds are filled</p>';
So, the problem here is that, all fields should be filled before the script can go to saying echo '<p>Required fileds are 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].' to continue<p/>';
break;
}else {
echo '<p>Required fileds are filled</p>';
}
}
endif;