Abbey Workshop

PHP: List Boxes with PHP

This form shows how to create combo, single select list boxes, and multiple select boxes. The form is submitted to a PHP page that handles the form data passed to it. How that data is handled is also discussed below.

Combo and List Box Form

Combo Box

List Box - Single Select

List Box - Multiple Select

Each of list box type is shown above.

Combo Box / Drop-down List

First the combo or drop down box is shown. This form of box includes multiple choices, but only 1 may be selected. In addition, only one option is shown in the list box itself. The for the box looks like this:

<p>Combo Box<br>
<select name="select">
<option value="Option 1" selected>Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>
</p>

The select tag identifies the name of the list box, and each option tag identifies both the value that is passed with the form, and the text that appears in the combo box.

List Box - Single Select

<p>List Box - Single Select<br>
<select name="listbox" size="3">
<option value="Option 1" selected>Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
</p>

This form is processed by the listbox.php script. You can see the code for this script as follows:

ListBox.php

   1:<html>
   2:<head>
   3:<title>List Box Form Data</title>
   4:</head>
   5:<body>
   6:<h3>List Box Form Data</h3>
   7:<p>Form data passed from the form</p>
   8:    <?php
   9:        echo "<p>select: " . $_POST['select']."</p>\n"; 
  10:        echo "<p>listbox: " . $_POST['listbox'] . "</p>\n";
  11:        $values = $_POST['listmultiple'];
  12:        echo "<p>listmultiple: ";
  13:        foreach ($values as $a){
  14:            echo $a;
  15:        }
  16:        echo "</p>\n";
  17:    ?>
  18:</body>
  19:</html>
  20: