0

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;

2 Answers 2

1

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 your output:

foreach($missing as $key)
  printf('<p>You must fill %s to continue</p>', $key);

if(!$missing)
  print '<p>Required fileds are filled</p>';
Sign up to request clarification or add additional context in comments.

4 Comments

When you click the submit button, it just says "You missed" and nothing is outputted.
So that means that all fields were filled. I've added a check to skip that message if so
Now, Almost good. But, when all three are field, it does not echo anything, or should I just add else statement to it
Thanks man. that was genius although you probably edited your ans 10 times :)
1
$needed = array("username", "lastname", "age");
$error = false;
$msg = '';

foreach ($needed as $value) {
    if(empty($_POST[$value]) {
      $msg =. 'please fill in ' . htmlspecialchars($_POST[$value]) .'<br/>';
      $error = true;
    }
}

if($error === true) {
  echo $msg;
} else {
  echo 'Great! finished';
}

2 Comments

@kranzdot: if you get a lot of errors, why not bother telling us what errors?
well, there is 1 in if(empty($_POST[$value]) you forgot one ), then 2 in l in ' . htmlsp unexpected . error and I forgot, there was 4 errors, and I just stoped. but thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.