[PHP Code > PHP Multiple Option Select Control]

Select Option Control

Picture of me and my dog

This demo shows the use of the HTML Multiple Selection-Option Control and how to use a PHP array to gather the user's input after a Submit. This also illustrates how to maintain and show what the user selected -- in other words, make the form sticky.


Please note: I have NOT included HTML labels for input statements. This was done to simplify the code and concept. If you are going to create forms for your websites, then you should also use labels to identify the HTML controls for the disabled. Please review the demos provided -- these show lables for all input controls requiring user action.

You may use the following script as you need without any obligation to me whatsoever. However, a Donation via PayPal would be greatly appreciated -- please note, this site does not accept advertising.


Here's the PHP/HTML code for the "Select Option" Control:

00 
07 <?php 08 09 error_reporting(E_ALL); // set error reporting to all 10 $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); 11 12 $option = array(); 13 $s = 'SELECTED'; 14 15 if(isset($_POST['option'])) 16 { 17 foreach ($_POST['option'] as $value) 18 { 19 $option[] = $value; 20 } 21 } 22 ?> 23 24 24 <!doctype html> 24 <html lang="en-us"> 24 24 <head> 24 <meta charset="utf-8"> 24 <title>tedd's Examples</title> 24 </head> 24 24 <body> 25 40 <form action="<?php echo($self); ?>" method="POST" class="examples"> 41 <p> 42 <select name="option[]" multiple size=5> 43 <option value="Apple" <?php if (in_array('Apple', $option)) echo($s); ?>>Apple</option> 44 <option value="Pear" <?php if (in_array('Pear', $option)) echo($s); ?>>Pear</option> 45 <option value="Orange" <?php if (in_array('Orange', $option)) echo($s); ?>>Orange</option> 46 <option value="Mango" <?php if (in_array('Mango', $option)) echo($s); ?>>Mango</option> 47 <option value="Apricot" <?php if (in_array('Apricot', $option)) echo($s); ?>>Apricot</option> 48 </select> 49 </p> 50 <p> 51 <input type="submit" name="submit" value="Submit"> 52 </p> 53 </form> 54 54 </body> 54 </html>
  Show the above CODE for Copy/Paste below.

The following code will work "as-is" without any additional coding. However, remember to use a .php' suffix.


<?php error_reporting(E_ALL); // set error reporting to all $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); $option = array(); $s = 'SELECTED'; if(isset($_POST['option'])) { foreach ($_POST['option'] as $value) { $option[] = $value; } } ?> <!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <title>tedd's Examples</title> </head> <body> <form action="<?php echo($self); ?>" method="POST" class="examples"> <p> <select name="option[]" multiple size=5> <option value="Apple" <?php if (in_array('Apple', $option)) echo($s); ?>>Apple</option> <option value="Pear" <?php if (in_array('Pear', $option)) echo($s); ?>>Pear</option> <option value="Orange" <?php if (in_array('Orange', $option)) echo($s); ?>>Orange</option> <option value="Mango" <?php if (in_array('Mango', $option)) echo($s); ?>>Mango</option> <option value="Apricot" <?php if (in_array('Apricot', $option)) echo($s); ?>>Apricot</option> </select> </p> <p> <input type="submit" name="submit" value="Submit"> </p> </form> </body> </html>