How to use <select multiple> with php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

How to use <select multiple> with php?

Post 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'];
?>
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

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

Post 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
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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.
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

Post 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" ).
Last edited by lcidw on Wed Jul 02, 2003 7:44 am, edited 2 times in total.
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

oops i forgot to take out the "['example']"

thanks for putting it right.
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

Post 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.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post 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.
Post Reply