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?
PHPAJAX

Avatar of undefined
Last Comment
Member_2_248744

8/22/2022 - Mon
Ray Paseur

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.
ASKER
Robert Granlund

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

srihari chander

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>
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur

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.
ASKER
Robert Granlund

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
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Member_2_248744

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.