BobFlemming
BobFlemming

Reputation: 2040

Multiple PHP dropdowns and getting the POSTed value from the array

I have , within a foreach , a dropdown:

`<select name="position[]">
<option value="1st">First</option>
<option value="2nd">Second</option>
<option value="3rd">Third</option>
</select>`

I need to be able to get the values from position[]when the form is posted

I had assumed it was $_POST['position'][0] , $_POST['position'][1] etc.

But that doesn't work .

Upvotes: 1

Views: 3823

Answers (3)

KJYe.Name
KJYe.Name

Reputation: 17169

Try this:

$test=$_POST['position'];
if ($test){
     foreach ($test as $t){
          echo 'You selected '.$t.'<br />';
     }
}

and also in select tag enable multiple selection by:

<select name="position[]" multiple="multiple"> 

Upvotes: 0

Daniel Cannon
Daniel Cannon

Reputation: 614

Try this:

<?php 
foreach($array as $key=>$value){ ?>
<select name="position[<?php echo $key; ?>]">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
<?php } ?>

You should then be able to access each select like this: $_POST['position'][$key]

Upvotes: 1

Gaurav
Gaurav

Reputation: 28755

You have not included multiple in html code of select.

You should use

<select name="name[]" multiple size"5">

Upvotes: 0

Related Questions