PHP Forms and User Input (Cont.)


PHP Drop-Down Lists (Single Selection)
Drop down lists are the basic selection forms. Drop down lists have several options a user can select.

HTML
13_1.php
 <form method="post" action="13_1.php">
  <select name="car">
   <option>Volvo</option>
   <option>Saab</option>
   <option>Mercedes</option>
   <option>Audi</option>
  </select>
  <input type="submit" name="act" value="Check">
 </form>
 <html><body>
 <?php
  echo "Pick: $_POST[car]";
 ?>
 </body></html>
     

PHP Drop-Down Lists (Multiple Selections)
The multiple attribute specifies that multiple options can be selected at the same time. Hold down the control (ctrl) button to select multiple options. The multiple attribute can be used together with the size attribute to define the number of options to be displayed.

HTML
13_2.php
 <form method="post" action="13_2.php">
  <select multiple="multiple"
     size="2" name="cars[ ]">
   <option>Volvo</option>
   <option>Saab</option>
   <option>Mercedes</option>
   <option>Audi</option>
  </select>
  <input type="submit" name="act" value="Check">
 </form>
 <html><body>
 <?php
 echo  "Pick: "; 
 $cars = $_POST[cars];
 foreach ( $cars as $car )
   echo  "$car ";
 ?>
 </body></html>