Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with multiple values of <select> in PHP?

<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?

like image 502
omg Avatar asked Feb 03 '23 09:02

omg


1 Answers

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
)
*/
like image 193
nickf Avatar answered Feb 06 '23 09:02

nickf