We help IT Professionals succeed at work.

PHP Query from Select option

Robert Granlund
Last Modified: 2016-04-12
Is there a way to query two different fields in a DB depending upon the select option?  I have a Select dropdown that has 6 options.  The first three are numbers and there is a field that is queried to see  if there are matching numbers in that field.  Then there are three Word options.  The words are in a different field.  Is there a way to create two different select clauses from one select option dropdown?
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Yes, of course!  Please show us the select tag and the options, and I'll be glad to show you the code to handle it.


It is very simple select, I just need to be able to query a specific field.  If it is 10 -30 field_1 and if red-blue field_2.  Or I guess if I can /  could create 2 different variables depending upon which is picked.

<select name="size_color" id="size_color">
 <option> value="10"> 10 <option>
  <option> value="20"> 20 <option>
  <option> value="30"> 30 <option>
  <option> value="red">Red <option>
  <option> value="green">Green<option>
  <option> value="blue">Blue<option>

Open in new window

srihari chanderSr. Software Engineer

Yes, we can achieve by calling the database query on the basis of the selected drop down value,
 function changeTest(obj){
    var selectedvalue=obj.options[obj.selectedIndex].value;

var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        xmlhttp.open("GET", "getfromdatabase.php", true);
 xmlhttp.open("GET", "getfromdatabase.php", true);


 <select name="level" onChange="changeTest(this)">
   <option value="1" selected="selected">1</option>
             <option value="2">2</option>
             <option value="3">3</option>
<option value="4">a</option>
<option value="5">b</option>
<option value="6">c</option>
Most Valuable Expert 2011
Author of the Year 2014

Please help me understand this a little more.  If the value is "10" then we want size = 10, and if the value is "red" then we want color = red?

If that's the case, a more intuitive design would have separate select controls for size and color.


It is not very intuitive and I have no say in it being two select dropdowns, which it should be. I am trying to simplify the example as much as possible.  I have used size and color to illustrate in theory not in actuality.  The product being sold  basically comes in 6 different options.  The first three options are all number based and they are kept in one table field.  The next three options are Word based (VARCHAR) and are kept in a separate table field.  The select menu is part of an AJAX call.  The first three options and the second three options in the select menu are represented in the AJAX call and are also part of the MySQL Query.  What I am not sure how to do is turn both sets into a variable to be passed along:  Like So

var number  = $('select[name="number"]').find(':selected').val();
var letter  = $('select[name="letter"]').find(':selected').val();

type: 'GET',
		url: '{site_url}/ajax/finder.php',
		data: {action:"finder", number: number, letter: letter},

<select name="number" data-placeholder="Select">
  			<option value="0" selected="selected">Select</option>
  			<option value="0">Any</option>
  			<option value="10">10</option>
  			<option value="20">200</option>
  			<option value="30">30</option>
                       <option value="red">red</option>
  			<option value="green">green</option>
  			<option value="blue">blue</option>

Open in new window

Can I possibly use "data" inside of the option attributes
Most Valuable Expert 2011
Author of the Year 2014
This one is on us!
(Get your first solution completely free - no credit card required)

greetings  rgranlund, , you can have 6 different values in the server PHP    $_GET['number']  , so you can then determine whether its a number or other (color), and then make a SQL string that will fit the SELECT for a number or color, maybe something like -

$num =(empty($_GET['number'])) ? "" : $_GET['number'];

switch($num) {
   case "0": $sql = 'SELECT * FROM products'; break;

   case "10": 
   case "20":
   case "30":  $sql = 'SELECT sizes FROM products where size = "'.$num.'"'; break;

   case "red":
   case "green":
   case "blue":   $sql = 'SELECT colors FROM products where color = "'.$num.'"'; break;


Open in new window

this is just DEMO code, the SQL strings are 4 example only and can not work, but you can use a PHP switch  block with fall thoroughs , that will have different SQL strings or change variables

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.