Jump to content

AJAX Database multiple search


garevn

Recommended Posts

Hello, according to this i found here http://www.w3schools.com/PHP/php_ajax_database.aspi was wondering how i can have more than one options for searching database.Atm i have no errors and i have results thats a good point.1) Atm the 2 options are not connected for example i want when i choose the 1st option 'intel' and then sochet 'sam3' then i have a message "no products found"my code

<?php require_once('Connections/Mysitedb.php'); ?><?php $q= (isset($_GET['q'])) ? $_GET['q'] : NULL;$t= (isset($_GET['t'])) ? $_GET['t'] : NULL; require_once('Connections/Mysitedb.php'); ?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {  if (PHP_VERSION < 6) {	$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  }  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);  switch ($theType) {	case "text":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;		case "long":	case "int":	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";	  break;	case "double":	  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";	  break;	case "date":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;	case "defined":	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;	  break;  }  return $theValue;}}mysql_select_db($database_Mysitedb, $Mysitedb);if ( $q and $t) {$query_Recordset1 = "SELECT * FROM products WHERE manufacturer = '" . $q . "' AND sochet = '" . $t . "'";  }else if ( $q ) {$query_Recordset1 = "SELECT * FROM products WHERE manufacturer = '" . $q .  "'";  }else if ( $t ) {$query_Recordset1 = "SELECT * FROM products WHERE sochet = '" . $t .  "'";}else {	echo ('no products');	return;}$Recordset1 = mysql_query($query_Recordset1, $Mysitedb) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);$totalRows_Recordset1 = mysql_num_rows($Recordset1);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><?php if($totalRows_Recordset1>0){?><?php do { ?>  <table>	<tr>	  <td width="132">price</td>	  <td width="138"><?php echo $row_Recordset1['price']; ?></td>	</tr>	<tr>	  <td>name</td>	  <td><?php echo $row_Recordset1['productname']; ?></td>	</tr>	<tr>	  <td>man</td>	  <td><?php echo $row_Recordset1['manufacturer']; ?></td>	</tr>	<tr>	  <td>sochet</td>	  <td><?php echo $row_Recordset1['sochet']; ?></td>	</tr>  </table>  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>   <?php }else echo "No records found";?></body></html><?phpmysql_free_result($Recordset1);?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript"> function showUser(str) { if (str=="")   {   document.getElementById("txtHint").innerHTML="";   return;   } if (window.XMLHttpRequest)   {// code for IE7+, Firefox, Chrome, Opera, Safari   xmlhttp=new XMLHttpRequest();   } else   {// code for IE6, IE5   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");   } xmlhttp.onreadystatechange=function()   {   if (xmlhttp.readyState==4 && xmlhttp.status==200)	 {	 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;	 }   } xmlhttp.open("GET","kailoipa3.php?q="+str,true); xmlhttp.send(); } </script> <script type="text/javascript"> function showUserAnd(str) { if (str=="")   {   document.getElementById("txtHint").innerHTML="";   return;   } if (window.XMLHttpRequest)   {// code for IE7+, Firefox, Chrome, Opera, Safari   xmlhttp=new XMLHttpRequest();   } else   {// code for IE6, IE5   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");   } xmlhttp.onreadystatechange=function()   {   if (xmlhttp.readyState==4 && xmlhttp.status==200)	 {	 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;	 }   } xmlhttp.open("GET","kailoipa3.php?t="+str,true); xmlhttp.send(); } </script> </head> <body> <form  name="products" id="prod"> <select name="products" onchange="showUser(this.value)"> <option value="" selected="selected">Select manuf:</option> <option value="intel">intel</option> <option value="amd">amd</option> </select>  <select name="products2" onchange="showUserAnd(this.value)"> <option value="">Select sochet:</option> <option value="s1156">s1156</option> <option value="sAM3">SAM3</option>  <option value="s1366">s1366</option> </select> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html>

Link to comment
Share on other sites

Ok justsomeguy i saw that example and i tried some.-I gave an id on each list.-Both lists run the same function.-I add some lines (wrong i think)This is my current code......:)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript"> function showUser(str) { if (str=="")   {	var q=document.getElementById("manuf").selectedIndex;	var t=document.getElementById("soch").selectedIndex;	document.getElementById("txtHint").innerHTML="";   return;   } if (window.XMLHttpRequest)   {// code for IE7+, Firefox, Chrome, Opera, Safari   xmlhttp=new XMLHttpRequest();   } else   {// code for IE6, IE5   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");   } xmlhttp.onreadystatechange=function()   {   if (xmlhttp.readyState==4 && xmlhttp.status==200)	 {	 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;	 }   } xmlhttp.open("GET","kailoipa5.php?q="+str,true); xmlhttp.open("GET","kailoipa5.php?t="+str,true) xmlhttp.send(); } </script> </head> <body> <form  name="products" id="prod"> <select name="products" id="manuf" onchange="showUser(this.value)"> <option value="" selected="selected">Select manuf:</option> <option value="intel">intel</option> <option value="amd">amd</option> </select>  <select name="products2" id="soch" onchange="showUser(this.value)"> <option value="">Select sochet:</option> <option value="s1156">s1156</option> <option value="sAM3">SAM3</option>  <option value="s1366">s1366</option> </select> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html>

Link to comment
Share on other sites

You put the lines in an if statement which returns instead of sending the ajax request. It would be better to do something like this:var q=document.getElementById("manuf");var q_val = q.options[q.selectedIndex].value;

Link to comment
Share on other sites

Damn i dont really get you...:)i should add in my code those lines as i understand in order to get the selected options but i am confused where to add them.

var q=document.getElementById("manuf");var q_val = q.options[q.selectedIndex].value; var t=document.getElementById("soch");var t_val = t.options[t.selectedIndex].value;

<script type="text/javascript"> function showUser(str) { if (str=="")   {  document.getElementById("txtHint").innerHTML="";   return;   } if (window.XMLHttpRequest)   {// code for IE7+, Firefox, Chrome, Opera, Safari   xmlhttp=new XMLHttpRequest();   } else   {// code for IE6, IE5   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");   } xmlhttp.onreadystatechange=function()   {   if (xmlhttp.readyState==4 && xmlhttp.status==200)	 {	 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;	 }   } xmlhttp.open("GET","kailoipa5.php?q="+str,true); xmlhttp.open("GET","kailoipa5.php?t="+str,true); xmlhttp.send(); } </script> </head> <body><form  name="products" id="prod"> <select name="products" id="manuf" onchange="showUser(this.value)"> <option value="" selected="selected">Select manuf:</option> <option value="intel">intel</option> <option value="amd">amd</option> </select>  <select name="products2" id="soch" onchange="showUser(this.value)"> <option value="">Select sochet:</option> <option value="s1156">s1156</option> <option value="sAM3">SAM3</option>  <option value="s1366">s1366</option> </select> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div></body> </html>

Is this line i have added above right?

xmlhttp.open("GET","kailoipa5.php?t="+str,true);

Link to comment
Share on other sites

Add them at the top of the function. You don't need to send this.value to the function any more, and it shouldn't have the str parameter. Instead it gets the values itself. And instead of sending 2 requests, it should send one with both values:xmlhttp.open("GET","kailoipa5.php?q=" + q_val + "&t=" + t_val, true);

Link to comment
Share on other sites

Add them at the top of the function. You don't need to send this.value to the function any more, and it shouldn't have the str parameter. Instead it gets the values itself. And instead of sending 2 requests, it should send one with both values:xmlhttp.open("GET","kailoipa5.php?q=" + q_val + "&t=" + t_val, true);
Y nice its like the example u post me before about getelementid "alert("Index: " + y[x].index + " is " + y[x].text);" i see.Ok then i erase the this value and str so look like this?
<script type="text/javascript"> function showUser() { if ("")   {var q=document.getElementById("manuf");var q_val = q.options[q.selectedIndex].value; var t=document.getElementById("soch");var t_val = t.options[t.selectedIndex].value;document.getElementById("txtHint").innerHTML="";   return;   } if (window.XMLHttpRequest)   {// code for IE7+, Firefox, Chrome, Opera, Safari   xmlhttp=new XMLHttpRequest();   } else   {// code for IE6, IE5   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");   } xmlhttp.onreadystatechange=function()   {   if (xmlhttp.readyState==4 && xmlhttp.status==200)	 {	 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;	 }   } xmlhttp.open("GET","kailoipa5.php?q=" + q_val + "&t=" + t_val, true); xmlhttp.send(); } </script> </head> <body> <form  name="products" id="prod"> <select name="products" id="manuf" onchange="showUser()"> <option value="" selected="selected">Select manuf:</option> <option value="intel">intel</option> <option value="amd">amd</option> </select>  <select name="products2" id="soch" onchange="showUser()"> <option value="">Select sochet:</option> <option value="s1156">s1156</option> <option value="sAM3">SAM3</option>  <option value="s1366">s1366</option> </select> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html>

Link to comment
Share on other sites

Remove that first if statement and just get the values first. You can check if both values are empty if you want to set the txtHint text and return, but the first thing you should do, outside of any if statement, is to get the values in the dropdowns.

Link to comment
Share on other sites

Remove that first if statement and just get the values first. You can check if both values are empty if you want to set the txtHint text and return, but the first thing you should do, outside of any if statement, is to get the values in the dropdowns.
LOL :) It works great man i will try add more options alone u were really helpfull.I wonder if its possible to display all possible results before selections
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...