SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Handling multiple selection drop down list box

You can read how drop down list boxes works for selecting one options out of many. You have also seen how the data of a pull down list box is handled inside a form. Here PHP is used to collect the selection of the form for further processing.

As you have seen in our processing script we collect the option selected by user by using appropriate GET or POST methods. Here is one example. Let us say the list box of colors we have used and the option selected by user is collected like this

$color=$_POST['color'];


or

$color=$_GET['color'];


Based on the form method ( GET or POST ) we can get or collect the option selected and store them in variable $color.

Same way let us see how we can get multiple selections from a drop down list box. Here is the demo of multiple selection pull down list box.

Here we get the selected values as array ( not as a single value ) , now this array we can loop through and display the elements. Here is the code to display the list box and then the collection of selected options.


Here is the code to handle the above script.
<form method=post action=''>
<select name='color[]' size=4 multiple>
<option value='blue'>Blue</option>
<option value='green'>Green</option>
<option value='red'>Red</option>
<option value='yellow'>Yelllow</option>
<option value='' selected>Select a Color </option>
<option value='white'>White</option>
</select>
<input type=submit></form>

///// collecting form data /////

@$color= $_POST['color'];
if( is_array($color)){
while (list ($key, $val) = each ($color)) {
echo "$val <br>";
}
}//else{echo "not array";}



Further readings
All form components validation with post back and data locking
Checkbox
How to retain form data if validation fails?
How to retain data of a text box after submitting
Retaining status of a check box
Updating checkbox value from & to a table record.
Retaining selected data of a period button of a form
Retaining selected data of a drop down list box of a form
Handling drop down list box with multiple selection options using array
Validating Date: Checking if date exist
Email address validation in a form
Email validation using Ajax & PHP
PHP Form Validation
is_numeric to check numeric numbers
ctype_alnum to check alphanumeric characters data
ctype_alpha to check alphabetic characters data
How to take care of form & query string variables if Register global is OFF?
















Join Our Email List
Email:  
For Email Newsletters you can trust
Form handling
PHP Sections