Page 1 of 1

How to use <select multiple> with php?

Posted: Wed Jul 02, 2003 7:27 am
by lcidw
I have created a <select> form-item. Now, it's possible to to select multiple values.

The question is: how to i catch all the selected values with php? The example below just gives back ONE value.. :(

EXAMPLE

Code: Select all

<?php
$contents = '<form name="fxample" method="post">
<select name="example" size="3" multiple="multiple">
    <option value="1">bike</option>
    <option value="2">car</option>
    <option value="3">train</option>
</select>
<input type="submit" value="Submit">
</form>';
echo $contents;
echo $_POST['example'];
?>

Re: How to use <select multiple> with php?

Posted: Wed Jul 02, 2003 7:30 am
by Tubbietoeter
try:

Code: Select all

<?php
$contents = '<form name="fxample" method="post">
<select name="example" size="3" multiple="multiple">
    <option value="1">bike</option>
    <option value="2">car</option>
    <option value="3">train</option>
</select>
<input type="submit" value="Submit">
</form>';
echo $contents;
echo "<pre>".print_r($_POST['example'])."</pre>";
?>


or try vardump

Posted: Wed Jul 02, 2003 7:33 am
by qartis
I'm pretty sure it's returned in an array, so $_POST['contents'][0] would be the first result. Try the above solution, print_r($_POST), first and tell us the output.

Posted: Wed Jul 02, 2003 7:40 am
by lcidw
You thought i didn't try that before?

No, i'm sorry, it really doesn't work, it just returnes the last one selected. print_r gives simply the name and value back of the last selected, var_dump says string(3) "two", print_r($_POST) says Array ( [example] => "two" ).

Posted: Wed Jul 02, 2003 7:40 am
by Tubbietoeter
oops i forgot to take out the "['example']"

thanks for putting it right.

Posted: Wed Jul 02, 2003 7:49 am
by lcidw
No one knows?

Ah, finally i found it. Here is how it's done, quite logical :lol:.
To pass values choosed via <select multiple> you have to use
<select multiple name="array[]"> construction.
But when you use 2 dimension array i.e. :
<select multiple name="array[0][]">
php does nothing with it.
So only 1 dimension array works with <select multiple>.
Thanks anyway, and i hope this will help one too.

Posted: Wed Jul 02, 2003 8:03 am
by qartis
Got it.

Instead of

Code: Select all

<select name="example" size="3" multiple="multiple">
Do

Code: Select all

<select name="example&#1111;]" size="3" multiple="multiple">


http://www.phpbuilder.com/board/showthr ... d=10237342

Posted: Wed Jul 02, 2003 8:05 am
by SBukoski
Well, for starters, the part multiple="multiple" is redundant in your code. You only need the phrase multiple to indicate you can select more than 1. Also, I always define my SELECTs with a name like "this_name[]". The brackets afterwards tells us that more than one will be selected, so we know it will be an array.

then loop through each one when it is posted. Here is an example of your select statment:

Code: Select all

<select name="example[]" size="3" multiple> 
    <option value="1">bike</option> 
    <option value="2">car</option> 
    <option value="3">train</option> 
</select>
and an example of how to display each of what is selected:

Code: Select all

while(list($examp_numb, $example) = each($_POST["example"])) {
    echo $example." (".$examp_numb.$")<br>";
}
This should output each example that was selected along with the number. Haven't tested it here, but I have very similar things in my code.