如何访问在 PHP 多选下拉列表中选择的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13455715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the values selected in the multiselect dropdown list in PHP?
提问by Allen
I am using Jquery Multiselect Widgetto have a dropdown list box with mulitselect option. I am populating the dropdown with the data from MySql database. I was not able to pass multiple values to the php file in $_POST.
我正在使用Jquery Multiselect Widget来创建一个带有 multiselect 选项的下拉列表框。我正在用 MySql 数据库中的数据填充下拉列表。我无法在 $_POST 中将多个值传递给 php 文件。
My HTML & PHP code for Multiselect DropDown.
我的多选下拉菜单的 HTML 和 PHP 代码。
<form id="intermediate" name="inputMachine" method="post">
<select id="selectDuration" name="selectDuration" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
</select>
<?php
//include '../db/interface/DB_Manager.php';
$connect = mysql_connect("localhost", "root", "infinit") or die(mysql_error());
mysql_select_db("serverapp") or die(mysql_error());
$query = "select id,name from rpt_shift_def"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="selectShift" name="selectShift" multiple="multiple">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
<option name="selected_ids[]" id ="<?php echo $fetch_options['id']; ?>" value="<?php echo $fetch_options['name']; ?>"><?php echo $fetch_options['name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
Here i'm populating Shift dropdown in Multiselect Dropdown. If i select more than one shift, i was not able to get all those selected values in php. instead i get only the last selected value.
在这里,我在多选下拉列表中填充 Shift 下拉列表。如果我选择了多个班次,我将无法在 php 中获得所有这些选定的值。相反,我只得到最后选择的值。
i want to do something like this.
我想做这样的事情。
$shiftarraycalc = array();
foreach ($_POST['selectShift'] as $key => $value) {
array_push($shiftarraycalc,$value);
}
but its not working.
但它不工作。
I have no idea how to get multiple values in $_POST.
我不知道如何在 $_POST 中获取多个值。
回答by Allen
Modified name as array in select
修改名称为选择中的数组
<select id="selectShift" name="selectShift[]" multiple="multiple">
and did like this and it works
并且这样做了并且它有效
$shiftarraycalc = array();
$shift=$_POST['selectShift'];
if ($shift)
{
foreach ($shift as $value)
{
array_push($shiftarraycalc,$value);
}
}
回答by Dulith De Costa
You could do like this too.
你也可以这样做。
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHPcode below. It print the selected multiple values accordingly.
然后从下面的PHP代码中进行多项选择。它相应地打印选定的多个值。
$shift=$_POST['selectDuration'];
print_r($shift);
回答by hanuman kumar
<select id="hanu" name="hanu[]" multiple="multiple">
<option> one</option>
<option> two </option>
<option> three</option>
<option> four </option>
<option> five</option>
<option> six </option>
<option> seven</option>
</select>`
And The php code for this is
而这个 php 代码是
$res_hanu = array();
$hanu_new=$_POST['hanu'];
if ($hanu_new){
foreach ($hanu_new as $value){
array_push($res_hanu,$value);
}
}
回答by aftab
$shift=$_POST['selectShift'];
if ($shift)
{
foreach ($shift as $value)
{
$shiftarraycalc[]=$value;
}
}

