PDA

Click to See Complete Forum and Search --> : Handling a Multiple Drop Down Box


noahssite
Aug 11th, 2009, 11:14 AM
Hello I have a <select name="dropbox" multiple="multiple" size="2"> in a form. When the user clicks the submit button the method used is POST.

I then handle the information by storing it in a variable.
$myMultiDropDownBox = $_POST['dropbox'];

I then tried to echo that variable. When multiple options were selected the echo wouldn't work. How do I handle the various items the user may have selected? Is it stored in an array? Hence I must do a loop?

SambaNeko
Aug 11th, 2009, 02:50 PM
For PHP, you'll need to declare the select name as an array...

<select name="dropbox[]" multiple="multiple" size="2">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

...in order to receive an array of the values...

print_r($_POST['dropbox']);
/* if you selected both options, this prints:

Array
(
[0] => 1
[1] => 2
)

*/
echo $_POST['dropbox'][0]; //echos 1
echo $_POST['dropbox'][1]; //echos 2