Jump to content

[SOLVED] Need Help With My AJAX/MySQL Query Search Script


phpQuestioner

Recommended Posts

I have a AJAX/PHP Database Search Script, but I cannot get it to send query accurately; I keep getting the code that I have in place to be displayed when nothing is found in the database by MySQL (ie. mysql_num_rows($result) == 0). I am typing in the make of the vehicle; just as I have it in my database; so this is not a typo error.

 

Could some take a look at it and tell me what is wrong with it?

 

 

HTML & AJAX Code

 

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your Does Not Support AJAX");
			return false;
		}
	}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.getElementById('vehicles').innerHTML = ajaxRequest.responseText;
	}
}
var age = document.getElementById('make').value;
var queryString = "?make=" + make;
ajaxRequest.open("GET", "ajax-search-routine.php" + queryString, true);
ajaxRequest.send(null); 
}

//-->
</script>



<form name="myForm">
<input type="text" id="make">
<input type="button" onclick="ajaxFunction()" value="Search">
</form>

<br><br>

<span id="vehicles"></span>

</body>
</html>

 

 

PHP Code

 

<?php

   print "<table width=700px class=tableContainer cellpadding=2 cellspacing=0 border=0>";

//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","username","password"); 

//select which database you want to edit
mysql_select_db("myVecs"); 

//select the table
$result = mysql_query("select * from vehicles where make='$make' order by year, make asc") or die("Inventory Database Connection Failed - Please Try Again Later");

if(mysql_num_rows($result) == 0){
print "<tr><td rowspan=5 style='color:red;font-family:arial;font-weight:bold;text-align:center'>Vehicle Inventory Out of Stock - Please Visit Us Again Soon - New Inventory Coming.....</td></tr>";
}

else {

//grab all the content
while($r=mysql_fetch_array($result))
{	
//the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $id=$r["id"];
   $year=$r["year"];
   $link=$r["link"];
   $make=$r["make"];
   $model=$r["model"];
   $series=$r["series"];
   $price=$r["price"];
   $price2=number_format($price);


//display the row
   print "<tr class=\"norm\" onmouseover=\"this.className='hover'; return true\" onmouseout=\"this.className='norm'; return true\" onclick=\"javascript:document.location='vehicle-description.php?id=$id'\" style=\"cursor:hand\"><td width=23% class=indTD><a href=\"vehicle-description.php?id=$id\" onmouseover=\"javascript:window.status='$year $make $model $series'; return true\" onmouseout=\"javascript:window.status=''; return true\">$make</a></td><td width=23% class=indTD>$model</td><td width=20% class=indTD>$series</td><td align=center width=16% class=indTD style=\"padding-left:0\">$year</td><td width=18% align=center class=vaiav><center><table width=60%><td style=\"text-align:left;width:40%\">$</td><td style=\"text-align:center;width:60%\">$price2</td></table></center></td></tr>";
  
}
     }

   print "</table></center>";

?>

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.