PHP

Checking Selection Lists

isset($_POST['listname']) && $_POST['listname'] != '')

When it comes to validating a selection list, the approach depends on the type of list:

  • If it is a list in which only one element may be selected, the list is considered to be filled out incorrectly if

  • No option in the list is selected

  • The selected option has an empty string as a value

  • If it is a list in which multiple elements may be selected, the list is considered to be filled out incorrectly if

  • No option in the list is selected

  • All selected options have an empty string as a value

Options with empty strings as values come into play if the list contains functionless dummy entries that have captions such as "Please choose from list." These list options must not receive a value other than "", so that the validation algorithm can distinguish these entries from reasonable ones.

The listing at the beginning of this tutorial validates a single selection listagain including PHP code to prefill the list.

Validating Lists (mandatory-list.php; excerpt)
<?php
  if (isset($_POST['Submit']) &&
      isset($_POST['listname']) &&
      $_POST['listname'] != '') {
    echo '<h1>Thank you for filling out this
      form!</h1>';
  } else {
?>
  <form method="post" action="<?php echo
     htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <select name="listname">
      <option value="">Please select from
        list</option>
      <option value="php3"<?php
  if (isset($_POST['listname'])
   && $_POST['listname'] == 'php3') {
    echo ' selected="selected"';
  }
      ?>>PHP 3</option>
      <option value="php4"<?php
  if (isset($_POST['listname'])
   && $_POST['listname'] == 'php4') {
    echo ' selected="selected"';
  }
      ?>>PHP 4</option>
      <option value="php5"<?php
  if (isset($_POST['listname'])
   && $_POST['listname'] == 'php5') {
    echo ' selected="selected"';
  }
      ?>>PHP 5</option>
    </select><br />
    <input type="submit" name="Submit" />
  </form>
<?php
  }
?>

With multiple lists, a bit more work is required, as shown in the following code. If the form has been submitted, the array of selected list elements is searched. If one element that is nonempty is found, the process is complete and the user can be congratulated for the successful completion of the form. Otherwise, the form is displayed again.

Validating Multiple Lists (mandatory-list-multiple.php; excerpt)
<?php
  $ok = false;
  if (isset($_POST['Submit']) &&
      isset($_POST['multilistname'])) {
    if (is_array($_POST['multilistname'])) {
      for ($i=0; $i <
        count($_POST['multilistname']); $i++) {
        if ($_POST['multilistname'][$i] != '') {
          $ok = true;
          break;
        }
      }
    }
  }
  if ($ok) {
    echo '<h1>Thank you for filling out this
      form!</h1>';
  } else {
?>
  <form method="post" action="
<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
   ?>">
    <select name="multilistname[]"
      multiple="multiple" size="5">
    </select>
<br />
    <input type="submit" name="Submit" />
  </form>
<?php
  }
?>

by BrainBellupdated
Advertisement: