Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

PHP Query from Select option

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?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of Robert Granlund

ASKER

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>
</select>

Open in new window

Yes, we can achieve by calling the database query on the basis of the selected drop down value,
<script>
 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;
            }
        };
if(selectedvalue<=3)
{
        xmlhttp.open("GET", "getfromdatabase.php", true);
}
else
{
 xmlhttp.open("GET", "getfromdatabase.php", true);
}
        xmlhttp.send();

  }
</script>

 <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>
 </select>
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

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

<AJAX>
$.ajax({
type: 'GET',
		url: '{site_url}/ajax/finder.php',
		data: {action:"finder", number: number, letter: letter},
</script>

<html>
<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>
  		</select>

Open in new window

Can I possibly use "data" inside of the option attributes
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;

   default: exit( "SERIOUS ERROR - PROCESSING STOPED");
  }

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