Allen Allen - 3 months ago 11
PHP Question

How to access the values selected in the multiselect dropdown list in 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.

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>





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.

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.

Answer

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