PDA

View Full Version : PHP: What will a SELECT with multiple selected values give?


iceangel89
06-15-2007, 07:26 AM
What will a <SELECT> with multiple selected values give? for example:

<SELECT>
<OPTION>Option 1</OPTION>
<OPTION>Option 2</OPTION>
<OPTION>Option 3</OPTION>
<SELECT>

Then i select Options 1 & 3, then what will the POST or GET output be like? i have no problems with 1 selected but if more that 1 is selected i think i will get an array?

danlefeb
06-15-2007, 07:41 AM
It will return whatever the value of the selected objects are :)

So, if you have:


<select name=MyAwesomeSelections multiple>
<option value="number01">Number 1 Is The Best</option>
<option value="number02">I Disagree, Number 2 Is The Best</option>
<option value="number03">On Third Thought, Number 3 Rocks</option>
</select>


And someone selects the first and the third options, then it will return "MyAwesomeSelections = number 01, number 03". Does that help?

iceangel89
06-15-2007, 11:07 AM
That means if the variable is $_POST['MyAwesomeSelections'], the string will be "number 01, number 03"? and if its GET, it will be filename.php?MyAwesomeSelections=number 01,number 03?

sorry abit noob here

What my lecturer did was name the SELECT "something[]" as an array, then the variable was $_POST['something']. then used a fornext loop to extract selected values, didn't quite understand that and it didn't work for me.

laxman
06-17-2007, 05:10 PM
You got the first part right.

Also MyAwesomeSelections in the above example is automatically converted into an array (i'm pretty sure)

Now, i've never heard of a fornext loop and a quick google search wasn't very promising. I think you mean a foreach loop. What that loop does is:

foreach ($w in $MyAwesomeSelection)
{
}

that assigns the fist value of the array to $w, so you can do what you want like print it or something, then when it loops back, it goes to the next entry in $MyAwesomeSelection and sets it to $w and so on...

Since this is a for you will want to stick with the POST method, but yes, both methods save the 'value' attribute, so make sure its something meaningful.

meltedpixels
12-18-2007, 07:13 PM
I thought I would just put you a file together so you can see the whole thing working. It's the same methods laxman pointed out.