Post results from a Multiple Select

I tried searching the forum - but unfortunately I kept getting server errors.

Here is my HTML:


<select name="drpYear[]" id="drpYearID" multiple="multiple" size="5">
<option value="2011" > 2011
<option value="2010" > 2010
<option value="2009" > 2009
<option value="2008" > 2008
<option value="2007" > 2007
</select>

this all appears fine on my page

However, when this gets POSTed - i’m only able to see the topmost selected item.


$cnt = count( $_POST[ "drpYear" ] );
$yrlist = $_POST[ "drpYear" ];
$yr = implode( ', ', $yrlist );

No matter how many I select, $cnt = 1 & $yr comes back with 2011 ( or whichever the highest one selected is ).

Now, everything I read said adding to the select name, and making the ID differ would work - but its not working for me.

Any ideas?

That doesn’t look right (but I’ve been bouncing between php and c# all day, so take that with a grain of salt…)

Try this:


<?php
$yearList = $_POST['drpYear'];
if ($yearList) {
   foreach ($yr as $yearList) {
      echo "<p>{$yr}</p>"; 
   }
}

You got your foreach backwards, Dave. foreach($yearList as $yr)

Thanks -

However - the implode is right and works fine ( I am using it elsewhere on the page in a similar way ( using multiple check boxes for the months ) ), and I’ve used it before on other items.

The issue is that $cnt = 1 - and it should equal 3 ( i’m testing with 3 years selected ). And $yr should equal 2011, 2009, 2007 as those are the ones i’ve selected. But my post[ ‘drpYear’ ] is not bringing those over.

Standard issue Response:

var_dump($_POST['drpYear']);

Result is…?

Result is:

array(1) {
[0]=>
string(4) “2011”
}

Again - select 2011, 2009, 2007

Forgive the dumb question, but how come in your example you didn’t close the option tags?

Probably because all of the examples I was using for Multiple Selects were older and didn’t have the </option> on it - so I didn’t think about it.

and that’d probably solve your problem…the problem doesnt seem to be in PHP, it’s in your HTML. Only 1 item is being passed on.

Well - I added that on - and it didn’t fix anything…

Here is the results from firebug in Mozilla for that section of the code:


<select name="drpYear[]" id="drpYearID" multiple="multiple" size="5"><option value="2011" >2011</option><option value="2009" >2009</option><option value="2008" >2008</option><option value="2007" >2007</option><option value="2010" >2010</option></select>

and here is my var_dump again:


array(1) {
  [0]=>
  string(4) "2011"
}

So - no difference with the </option>

<form action="" method="POST">
<select name="drpYear[]" id="drpYearID" multiple="multiple" size="5">
<option value="2011" > 2011</option>
<option value="2010" > 2010</option>
<option value="2009" > 2009</option>
<option value="2008" > 2008</option>
<option value="2007" > 2007</option>
</select>
<input type="submit" name="submit" value="POST" />
</form>

I just ran this and ran print_r($_POST) and got all options I selected. Can you trying running this and tell us what you get?