Forum Moderators: coopster

Message Too Old, No Replies

How to handle multiple select boxes

         

stidj

6:33 am on Nov 12, 2003 (gmt 0)

10+ Year Member



If you have code like below and you are using php.
HOw would you access each selection?

<td>
<select name="categories" multiple size="10" id="categories">
<option value="catchoose">Choose a category</option>
<option value="Electronics">Electronics</option>
<option value="Housewares">Housewares</option>
<option value="Hardware">Hardware</option>
</select>
</td>

DrDoc

6:47 am on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to make categories an array...

<select name="categories[]" multiple size="10" id="categories">

dreamcatcher

6:51 am on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi stidj,

You could also use an if statement, ie:

if ($categories =="catchoose")

{

do actions;

}

else if ($categories == "Electronics")

{

do actions;

}

and so on....or you could use a switch statement:

switch ( $categories )

{

case "catchoose":
echo "something...";
break;
case "Electronics":
echo "something...";
break;
case "Housewares":
echo "something...";
break;
case "Hardware":
echo "something...";
break;
default:
print "Nothing was selected!";
break;

}

Hope that helps!

:)

coopster

12:37 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The other folks here have already posted the answers, I'll just tell you where you can find them in the PHP Manual.

PHP and HTML FAQ [us2.php.net].

Timotheos

5:45 pm on Nov 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was a bit non-plussed to find that checkboxes needed to have the [] to make it an array. Such is not the case with Perl so what's the difference? I found this out while trying to wean myself from Matt's formmail Perl script to my own custom php formmail. Now I have to go back through all my forms and update all the check boxes. What a pain! Anybody have any suggestions to make it less of a hassle.