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.

4
  • What's the contents of _POST['select']? Commented Feb 3, 2016 at 6:04
  • remove [] from html tag, if there is only one select tag Commented Feb 3, 2016 at 6:05
  • Check this link for more help... stackoverflow.com/questions/2407284/… Commented Feb 3, 2016 at 6: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. Commented Feb 3, 2016 at 6:23

4 Answers 4

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);
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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
You cannot store array in DB. So you need to use implode method. Write $result = implode(',',$data); Now store $result in DB
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

Comments

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

you can check it by

print_r($_POST['select']);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.