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

天涯浪子 提交于 2020-01-02 02:11:05

问题


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


回答1:


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
)
*/



回答2:


Starting Html file. ex. a.html

<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'] );
?>


来源:https://stackoverflow.com/questions/1452914/how-to-deal-with-multiple-values-of-select-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!