1

I have a multiple select dropdown field in html which has to return multiple values but it is returning only single value.

 <select name="select[]"  multiple>
 <OPTGROUP label="cars">
 <option value="car1">car1</option>
 <option value="car2">car2</option>
 <option value="car3">car3</option>
 </OPTGROUP>
 <OPTGROUP label="bikes">
 <option value="bike1">bike1</option>
 <option value="bike2">bike2</option>
 <option value="bike3">bike3</option>

Below is the PHP to return the multiple values that are selected:

 $select = mysqli_real_escape_string($link, $_POST['select']);

Thanks in advance.

user5358888
  • 207
  • 1
  • 12
  • What's the contents of `_POST['select']`? – u_mulder Feb 03 '16 at 06:04
  • remove [] from html tag, if there is only one select tag – Gaurav Rai Feb 03 '16 at 06:05
  • Check this link for more help... http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – Gagan Upadhyay Feb 03 '16 at 06:09
  • `$_POST['select']` is an array. You can't do `mysqli_real_escape_string()` on an array, as it expects param 2 to be a string. You need to loop over each value to do escape, without overwriting the previous value. – Sean Feb 03 '16 at 06:23

4 Answers4

1

Your code looks ok. Write select tag as below:-

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

Now print it:-

    if(isset($_POST['select'])){
        $selected = $_POST['select']; 
        // Here $selected is an array. So need a foreach loop            
        foreach ($selected as $option)
        {
             print "You are selected $option<br/>";
             //print mysqli_real_escape_string($link, $option);
        }
    }
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • when i use print or echo am able to get all the vlaues but when i assign mysqli_real_escape_string($link, $option); to $example to insert in db i am getting only one value. – user5358888 Feb 03 '16 at 07:44
  • You're welcome :) You need an array to store all $option. Try this code :- $data[] = mysqli_real_escape_string($link, $option); and print_r($data) at end. – Ravi Hirani Feb 03 '16 at 08:43
  • if(isset($_POST['select'])){ $selected = $_POST['select']; // Here $selected is an array. So need a foreach loop foreach ($selected as $option) { $data[] = mysqli_real_escape_string($link, $option); } } i have inserted this part of code but when iam inserting $data into the db column am not getting all the values.Please help in this – user5358888 Feb 03 '16 at 09:13
  • You cannot store array in DB. So you need to use implode method. Write $result = implode(',',$data); Now store $result in DB – Ravi Hirani Feb 03 '16 at 09:16
0

there is two things you have to do one first check your form method if it is not POST then you can't call it like $_POST you have to use $_GET or $_REQUEST and change select[] to select

shyam sasi
  • 97
  • 6
0
<select name="select[]" multiple="multiple">

you can check it by

print_r($_POST['select']);
Junaid
  • 592
  • 1
  • 7
  • 24
0
<select name="select[]" multiple="multiple">
if(isset($_POST['select'])){
    $selected = $_POST['select'];
    $select = implode(",",$selected);
}

use implode method, to insert $select values in database ....