Categories
PHP

Validating Selection Lists

How to create a dropdown list from an array, validate selection lists using the in_array() function and validate multiple lists with array_diff() function.

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
    1. No option in the list is selected
    2. The selected option has an empty string as a value
    3. The selected option does not exist in the database (or script)
  • If it is a list in which multiple elements may be selected, the list is considered to be filled out incorrectly if
    1. No option in the list is selected
    2. All selected options have an empty string as a value
    3. The selected option does not exist in the database (or script)

Example: Creating a selection list from an array

<?php
 $options = array('ASP','CSS','PHP','JSP','HTML','JavaScript');
?>
<form method="post" action="">
 <select name="list">
  <option value="">Please select from list</option>
<?php
 foreach ($options as $option) {
  echo "<option>$option</option>\n";
 }
?>
 </select>
 <input type="submit" value="Submit" name="submit">
</form>

Validating selection list:

<?php
 $options = array('ASP','CSS','PHP','JSP','HTML','JavaScript');

 // Checking if form has been submitted
 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $item = isset($_POST['list']) ? $_POST['list'] : '';
  if ( in_array($item, $options) ) {
   echo '<p><b>Thank you for filling out this form!</b></p>';
  }
  else {
   echo '<p style="color:red"><b>Error! Please select an item.</b></p>';
  }
 }
?>
<form method="post" action="">
 <select name="list">
  <option value="">Please select from list</option>
<?php
 foreach ($options as $option) {
  echo "<option>$option</option>\n";
 }
?>
 </select>
 <input type="submit" value="submit" name="submit">
</form>

Validating Multiple Lists

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 error message is displayed.

<?php
 $options = array('ASP','CSS','PHP','JSP','HTML','JavaScript');
 $items = array();
 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $items = isset($_POST['list']) ? $_POST['list'] : array();

  $diff = array_diff($items, $options);

  if (count($diff) == 0 && count($items) != 0) {
   echo '<p><b>Thank you for filling out this form!</b></p>';
  }
  else {
   echo '<p style="color:red"><b>Error! Invalid option(s) found.</b></p>';
  }
 }
?>
<form method="post" action="">
 <select name="list[]" multiple>
  <option value="">Please select from list</option>
<?php
 foreach ($options as $option) {
  $selected = in_array($option, $items) ? 'selected' : '';
  echo "<option $selected>$option</option>\n";
 }
?>
 </select>
 <br>
 <input type="submit" value="submit" name="submit">
</form>

For more detail visit Form Validation with PHP tutorial.


Processing Forms in PHP: