如何访问PHP的multiselect下拉列表中选择的值? [英] How to access the values selected in the multiselect dropdown list in PHP?

查看:111
本文介绍了如何访问PHP的multiselect下拉列表中选择的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Jquery Multiselect Widget to 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.

我的HTML& Multiselect DropDown的PHP代码.

My HTML & PHP code for Multiselect DropDown.

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

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

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.

我想做这样的事情.

   $shiftarraycalc = array();

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

但无法正常工作.

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

I have no idea how to get multiple values in $_POST.

推荐答案

在select中将名称修改为数组

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);
    }
}

这篇关于如何访问PHP的multiselect下拉列表中选择的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆