php2learn Posted June 2, 2014 Share Posted June 2, 2014 I have a sql query similar to below in a .php file. $sql = "select id,organisation,price from table where category = '$category'; These are the four unique values of the category column for your reference. "A-t1" "B-t1" "C-t1" "D-t1" Now I want to create a dropdown list or listbox in .php file with option to select multiple values. If customer selects multiple values, it should fetch query for the selected categories. E.g. dropdown list should be similar to below : "A-t1" "B-t1" "C-t1" "D-t1" If an user selects "A-t1" and "C-t1", it should give output/query for the selected categories. Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/ Share on other sites More sharing options...
gizmola Posted June 2, 2014 Share Posted June 2, 2014 Ok, what have you tried? Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481647 Share on other sites More sharing options...
fastsol Posted June 2, 2014 Share Posted June 2, 2014 You start by putting the multiple="multiple" attribute in the select box tag. Next add [] to the end of the name value in the select box to tell php that it should accept this as an array. Then when the form is posted, the post field will come across as an array under the name of the select box. Here's a basic example. <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Untitled 1</title> </head> <body> <?php if(isset($_POST['submit'])) { print_r($_POST); } ?> <form action="" method="post"> <select multiple="multiple" name="test[]"> <option>item1</option> <option>item2</option> <option>item3</option> <option>item4</option> </select> <input type="submit" name="submit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481650 Share on other sites More sharing options...
php2learn Posted June 2, 2014 Author Share Posted June 2, 2014 (edited) @gizmola As of now, I have the following following php file. <?php $category = isset( $_REQUEST['category']) ? $_REQUEST['category'] : null; <?> <body> <form action = $_SERVER['PHP_SELF'] name = "form1" method = "post"> <select name="category" multiple = "multiple"> <option value = "A-t1"> A-T1 </option> <option value = "B-t1">B-T1 </option> <option value = "C-t1">C-T1 </option> <option value = "D-t1">D-T1 </option> Edited June 2, 2014 by php2learn Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481652 Share on other sites More sharing options...
php2learn Posted June 2, 2014 Author Share Posted June 2, 2014 Thank you. For your information, I'm new to PHP stuff. I already saw similar kind of examples online in many forum but I can't quite understand how I can relate that for my need. For e.g. in your example, you haven't set any variable but I want $category to be set. You start by putting the multiple="multiple" attribute in the select box tag. Next add [] to the end of the name value in the select box to tell php that it should accept this as an array. Then when the form is posted, the post field will come across as an array under the name of the select box. Here's a basic example. <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Untitled 1</title> </head> <body> <?php if(isset($_POST['submit'])) { print_r($_POST); } ?> <form action="" method="post"> <select multiple="multiple" name="test[]"> <option>item1</option> <option>item2</option> <option>item3</option> <option>item4</option> </select> <input type="submit" name="submit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481654 Share on other sites More sharing options...
fastsol Posted June 2, 2014 Share Posted June 2, 2014 You have syntax errors in the code you posted. If the form is going to post to the page it's on, then just leave the action="". Also it is not suggested to use $_REQUEST because it can include both $_GET and $_POST which is not a good thing. You should always specifify the method you want to use. <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Untitled 1</title> </head> <body> <?php if(isset($_POST['submit'])) { $category = $_POST['category']; print_r($category); } ?> <form action="" method="post"> <select multiple="multiple" name="category[]"> <option>item1</option> <option>item2</option> <option>item3</option> <option>item4</option> </select> <input type="submit" name="submit" value="Submit"> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481659 Share on other sites More sharing options...
Psycho Posted June 2, 2014 Share Posted June 2, 2014 @php2learn, Start with what fastsol posted about changing the SELECT input field so it will accept multiple selections and return an array to the PHP processing page. Once you have completed that, you will then need to change the code on the processing page accordingly. First you will need to add validation logic to all the values int he array. Then, you can change your query to use the IN condition as opposed to the equal (=) condition. An IN condition looks something like this SELECT * FROM table WHERE field IN (1, 3, 5, 9) That will return a result set where the field has a value of any of the comma separated values listed. So, you will need to validate the array of values passed and then put them into a comma separated list. Note: since you are using 'text' values they need to be enclosed in single quote marks int he query that you build, e.g. SELECT id, organisation, price FROM table WHERE category IN ('A-t1', 'C-t1') Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481668 Share on other sites More sharing options...
php2learn Posted June 2, 2014 Author Share Posted June 2, 2014 @fastsol I managed to use your code and it prints the category in array. I'm not sure about the next step how to pass the array of values into the comma separated list as suggested by Psycho. If possible, you provide me the code. Otherwise, I will keep trying and let you know if I succeed. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481684 Share on other sites More sharing options...
fastsol Posted June 2, 2014 Share Posted June 2, 2014 $category = $_POST['category']; // Replace this line with the one below. That will give you a comma separated list for the IN() in the query $category = implode(',', $_POST['category']); Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481686 Share on other sites More sharing options...
Solution Psycho Posted June 2, 2014 Solution Share Posted June 2, 2014 $category = $_POST['category']; // Replace this line with the one below. That will give you a comma separated list for the IN() in the query $category = implode(',', $_POST['category']); That would not work . . . Note: since you are using 'text' values they need to be enclosed in single quote marks int he query that you build, Plus, doing the above completely circumvents any validation logic. You don't show any of your original code for generating the query, so it is impossible to provide a valid solution. But, to use what fastsol provided you could do this $categories = "'" . implode("', '", $_POST['category']) . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481690 Share on other sites More sharing options...
fastsol Posted June 2, 2014 Share Posted June 2, 2014 Yeah I forgot the quote part on the implode example. And for sure any validation becomes next to impossible with this way but like Pyscho says you haven't provided enough code/info to demonstrate that based on your needs. Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481693 Share on other sites More sharing options...
php2learn Posted June 4, 2014 Author Share Posted June 4, 2014 @fastsol & Psycho, The code worked for me. Thanks a lot. Sure, next time I will provide as much information possible to demonstrate what I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/288937-php-code-for-selecting-multiple-values-from-dropdown/#findComment-1481859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.