问题
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>
<?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.
回答1:
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);
}
}
回答2:
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 PHP code below. It print the selected multiple values accordingly.
$shift=$_POST['selectDuration'];
print_r($shift);
回答3:
<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
$res_hanu = array();
$hanu_new=$_POST['hanu'];
if ($hanu_new){
foreach ($hanu_new as $value){
array_push($res_hanu,$value);
}
}
回答4:
$shift=$_POST['selectShift'];
if ($shift)
{
foreach ($shift as $value)
{
$shiftarraycalc[]=$value;
}
}
来源:https://stackoverflow.com/questions/13455715/how-to-access-the-values-selected-in-the-multiselect-dropdown-list-in-php