Click to See Complete Forum and Search --> : form passing select options (multiple) not working
bobby_dummy_022
11-26-2003, 09:46 PM
this is what I have:
<FORM Action="test.php" MULTIPLE>
<SELECT Name="fruits" size="3">
<OPTION value="1">Apple</OPTION>
<OPTION value="2">Orange</OPTION>
<OPTION value="3">Pineapple</OPTION>
<OPTION value="4">Grape</OPTION>
</SELECT>
</FORM>
my problem is, when I submit the form, I only get the value of the last selected item (despite the fact that the user can select multiple options frmo the list).
$_POST["fruits"] = (the last of the selected options)
I need to get the values of all of the selected options. How?
bobby_dummy_022
11-26-2003, 09:49 PM
In addition to my previous post, for example: the user can selected pineapple, apple, and orange. When the form is submitted, the php variable $_POST["fruits"] is assigned the value "1".
Where infact I need the values "1", "2", "3" - because the user selected pineapple, apple, and orange. I checked using the php method array_keys() to see if the variable $_POST["fruits"] was an array, and it is not.
Easy answer, make it an array. Naming it fruits[] should do so...
bobby_dummy_022
11-26-2003, 10:07 PM
I just tried that, and Iam getting the same problem. So you are suggesting make it like this:
<FORM Action="test.php" MULTIPLE>
<SELECT Name="fruits[]" size="3">
<OPTION value="1">Apple</OPTION>
<OPTION value="2">Orange</OPTION>
<OPTION value="3">Pineapple</OPTION>
<OPTION value="4">Grape</OPTION>
</SELECT>
</FORM>
is this what you meant?
bobby_dummy_022
11-26-2003, 10:10 PM
Just a correction, my form is declared with method="post"
like:
<FORM Action="test.php" method="post" MULTIPLE>
<SELECT Name="fruits[]" size="3">
<OPTION value="1">Apple</OPTION>
<OPTION value="2">Orange</OPTION>
<OPTION value="3">Pineapple</OPTION>
<OPTION value="4">Grape</OPTION>
</SELECT>
</FORM>
You have a few problems with you HTML. Try this:
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><select name="fruits[]" size="3" multiple="multiple">
<option value="1">Apple</option>
<option value="2">Orange</option>
<option value="3">Pineapple</option>
<option value="4">Grape</option>
</select>
<input type="submit" name="submit" value="submit"></p>
</form>
<?PHP
if (isset($_POST['submit'])) {
foreach ($_POST['fruits'] as $fruit) {
echo $fruit."<br>";
}
}
?>