views:

254

answers:

2
<select id="industryExpect" multiple="multiple">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
...
</select>

I'm seeing something like : industryExpect=13&industryExpect=17&industryExpect=19

Say,there are multiple value with the same KEY,

How to retrieve them from $_POST in PHP?

A: 
<form action="process.php" method="post">
    <select name="names[]" multiple="multiple">
    <option value="john">john</option>
    <option value="jack">jack</option>
    </select>
    <input type=hidden name=submitted>
    <input type=submit name=submit>
</form>

Process.php:
<?php

print_r( $_POST['names'] );
meder
Tried,not working.
Shore
Have you tried making it work? Try now.
meder
@meder You're a glutton for punishment.
random
+3  A: 

Give it a name with [] on the end, for example:

<select id="industryExpect" name="industryExpect[]" multiple="multiple">
    <option value="1">item1</option>
    <option value="2">item2</option>
    <option value="3">item3</option>
</select>

Then in your $_POST array, you'll get an array of all the selected options:

// assuming that item1 and item3 were selected:
print_r($_POST['industryExpect']);
/*
array
(
    0 => 1
    1 => 3
)
*/
nickf
Shore
Oh,it works,very strange
Shore
Mine didn't work?
meder
So it's the standard that when there is %5B%5D in key,treat as array?
Shore
@meder ,it also works like charm:)
Shore
if you're using `method="GET"` (which is the default), then your values will be added to the URL and that'd be why it's getting encoded. Of course, then you'd just have to use `$_GET` instead of `$_POST`
nickf