How to access the values ​​selected in the multiselect drop-down list in PHP?

I am using the Jquery Multiselect Widget to have a dropdown with the mulitselect parameter. I populate the drop-down list with data from the MySql database. I could not pass multiple values ​​to the php file in $ _POST.

My HTML and 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 fill in the Shift drop-down menu in the Multiselect drop-down list. If I select more than one shift, I could not get all these selected values ​​in php. instead, I only get the last selected value.

I want to do something like this.

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

but does not work.

I do not know how to get multiple values ​​in $ _POST.

+4
source share
4 answers

The modified name as an array in select

 <select id="selectShift" name="selectShift[]" multiple="multiple"> 

and did it and it works

 $shiftarraycalc = array(); $shift=$_POST['selectShift']; if ($shift) { foreach ($shift as $value) { array_push($shiftarraycalc,$value); } } 
+12
source

You can do it 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 grab a few options from the following PHP code. It prints the selected multiple values ​​accordingly.

 $shift=$_POST['selectDuration']; print_r($shift); 
0
source
 <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

 $res_hanu = array(); $hanu_new=$_POST['hanu']; if ($hanu_new){ foreach ($hanu_new as $value){ array_push($res_hanu,$value); } } 
0
source
 $shift=$_POST['selectShift']; if ($shift) { foreach ($shift as $value) { $shiftarraycalc[]=$value; } } 
-1
source

Source: https://habr.com/ru/post/1446964/


All Articles