Jump to content

php code for selecting multiple values from dropdown


php2learn
Go to solution Solved by Psycho,

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

@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 by php2learn
Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

@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')
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution
$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']) . "'";
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.