How does PHP handle multiple selects in <select> tags?


#1

How does PHP handle multiple selects in tags?

Here?s my problem; on a project I?m current working on, I need to able to select multiple states in the US to run a SQL query on. Since HTML tag supports multiple selections I decided to use it and see what is returned when I submit the form. To my surprise it only returns the selected value lowest on the list. I created a test script (posted below) to test this out further.

For example please select the follow; Alaska, DC, New York, Wyoming, and hit submit. In this case, only WY will be displayed.

How can I get it so it will post all the values that were selected? Any help would be appreciated.

Test Script
[php]

<?php if($submit) { echo $_POST[state]; } else { ?> Untitled Document Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware DC Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming <?php } ?> [/php]

#2

[code]<?php

if(isset($_POST[‘submit’]))
{
foreach ($_POST[state] as $state)
{
echo $state;
}
}
else
{
?>

Untitled Document Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware DC Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming <?php } ?> [/code]

#3

I get an invalid argument supplied for foreach() when I run the code posted above. The only thing I can think of is that $_POST[state] is not an array. Any ideas?


#4

I could be wrong, but I’m betting it is because you didn’t actually look through for other changes I’d made besides to the PHP part.


#5

Yup, my bad. Just got too excited on the foreach loop that I didn’t stop to look at the actual HTML section. Thanks again!