View Full Version : Select Multiple Options

12-14-2006, 07:38 PM
I have a webpage I have made, to filter a list of emails. Currently it is set up to select by Branch and/or Group, and Branch and Group are drop downs where you can pick one. I would like to change it so that you can pick multiple ones, so I switched the page over so that it is a multiple select, but when it actually runs the query, it only outputs based on one of them.

So the url result shows: "choice=request&search=03&search=04&search2=Q" and the list is only showing the search=04 people, it is ignoring the first search= part.

I am not at all sure how to go about fixing this, or if I am even going about this the right way. I would appreciate any pointers. Thank you.

Here is some of the relevant code:

<FORM METHOD="get" ACTION="emails2.php">

<INPUT TYPE=hidden NAME=choice VALUE="request">
<form name="form1" method="get" action="emails2.php">
Select a Branch:
<select multiple name="search" size=16>
<option value="">All Branches</option>
<option value="03">03 - Portland</option>
...list of options...
Select a Mail Group:
<select multiple name="search2" size=21>
<option value="">All Groups</option>
<option value="A">A - Corporate Administration</option>
...list of options...
<INPUT TYPE="submit" VALUE="Search!" class="btn">

if ($choice == "request")
echo "All Selected Emails";
$result =
mysql_query("SELECT * FROM emails
WHERE (Branch LIKE \"%$search%\" OR Branch = 'ALL')
AND (Mailgroup LIKE \"%$search2%\")
ORDER BY Name");

12-14-2006, 08:32 PM
So the url result shows: "choice=request&search=03&search=04&search2=Q" and the list is only showing the search=04 people, it is ignoring the first search= part.

As PHP reads the query string, it adds name/value pairs to the $_GET superglobal. If a name occurs twice, the existing value is overridden.

To get around this, you can add a pair of square brackets after the name. The name will remain the same, but PHP will create an array into which it will insert each value.

So, in the markup,

<select name="search" ...>


<select name="search[]" ...>

The data will still be available from $_GET['search'], but the value won't evaluate to a string, it will be an array.

SELECT * FROM emails
&#160;&#160;&#160;&#160;WHERE (Branch LIKE \"%$search%\" OR Branch = 'ALL')
&#160;&#160;&#160;&#160;AND (Mailgroup LIKE \"%$search2%\")
&#160;&#160;&#160;&#160;ORDER BY Name

That query doesn't seem capable of handling multiple search terms. Even if the value of $search was a long string of options - and it won't be - it wouldn't permit searching on each space-separated value within it.

Hope that helps,

08-10-2007, 10:17 AM
Cheers for that Mike, that worked like a charm.

08-13-2007, 12:54 PM
what if I have an invalid query?

Hank Freid
05-19-2008, 05:31 AM
you can try this. Hopefully solved the problem.

<script type="text/javascript">
function formAction(){
var x=document.getElementById("aSelectControl")

<select id="aSelectControl" size="3">
<input type="button" onclick="formAction()" value="Set to

Select multiple">
Hank Freid (http://www.commercialpropertynews.com/cpn/article_display.jsp?vnu_content_id=1003156655)

the developer hank freid (http://www.impulsivegroup.com/html/about_bios.html) redefines the chic hotel experience