如何访问PHP中多选下拉列表中选择的值?

时间:2012-11-19 14:21:39

标签: php multi-select

我正在使用Jquery Multiselect Widget来获得带有mulitselect选项的下拉列表框。我正在使用MySql数据库中的数据填充下拉列表。我无法将多个值传递给$ _POST中的php文件。

我的HTML& Multiselect DropDown的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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


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

 

这里我在Multiselect Dropdown中填充Shift下拉列表。如果我选择多个班次,我无法在php中获得所有这些选定的值。相反,我只得到最后一个选定的值。

我想做这样的事情。

   $shiftarraycalc = array();

foreach ($_POST['selectShift'] as $key => $value) {
    array_push($shiftarraycalc,$value);
}

但它不起作用。

我不知道如何在$ _POST中获取多个值。

3 个答案:

答案 0 :(得分:12)

修改名称作为选择

中的数组
<select id="selectShift" name="selectShift[]" multiple="multiple">

并且这样做并且有效

$shiftarraycalc = array();
$shift=$_POST['selectShift'];

if ($shift)
{
    foreach ($shift as $value)
    {
        array_push($shiftarraycalc,$value);
    }
}

答案 1 :(得分:0)

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

这个的PHP代码是

$res_hanu = array();
$hanu_new=$_POST['hanu'];
if ($hanu_new){
foreach ($hanu_new as $value){
array_push($res_hanu,$value);
}
}

答案 2 :(得分:-1)

$shift=$_POST['selectShift'];

if ($shift)
{
    foreach ($shift as $value)
    {
        $shiftarraycalc[]=$value;
    }
}