faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input : Validating Data

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

42 of 79 people (53%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

Is there a quick way to check that all form fields were entered?

Jan 23rd, 2000 20:03
Nathan Wallace, Richard Lynch


Try something like this that just checks to see that all the POST
variables are not empty.

    <?php
      if (isset($HTTP_POST_VARS)){
        while(list($var, $val) = each($HTTP_POST_VARS)){
          if (empty($val)){
            $bad[$var] = 1;
      } } }
    ?>
    <FORM ...>
      <?php
        if (isset($bad)){
          echo "<FONT COLOR=FF0000>All Fields Required</FONT>";
        }
      ?>
      <?php if (isset($bad['whatever'])){
        echo "<FONT COLOR=FF0000>";
      ?>
      Whatever: <INPUT NAME=whatever>
    </FORM>