Jump to content

AJAX search


ra_ie_darkness

Recommended Posts

Hello,

I am new to ajax and javascript and am trying to create a search engine 

 

This is the javascript code





var XMLHttpRequestObject = false;
if(window.XMLHttpRequest)
{
	XMLHttpRequestObject = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function showresult(search)
{
	if(XMLHttpRequestObject)
	{
		var obj = document.getElementById("showresult");
		XMLHttpRequestObject.open("POST","index.php",true);
		XMLHttpRequestObject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		XMLHttpRequestObject.onreadystatechange = function()
		{
			if(XMLHttpRequestObject.readystate == 4 $$ XMLHttpRequestObject.status == 200)
			{
				obj.innerHTML = XMLHttpRequestObject.responseText;
			}
		}
		XMLHttpRequestObject.send("search=" + search);
	}
	//alert("You clicked me");
}

 

PHP + HTML:





<form method="post" name ="searchform" id="idsearchform" >
<input type="text" name="search"/>
<input type="button" name="starts" value="search" onclick="fun(search)"/>
</form>
<?php
if(isset($_POST['starts']))
{
	$keyword = $_POST['search'];
	$search_q = mysql_query("Select * from products where pname like '%$keyword%'");
	if(mysql_num_rows($search_q)!=0)
	{
		while($result = mysql_fetch_array($search_q))
		{
?>
<div id="showresult">
<?php
			$name = $result['pname'];
			echo "$name<br/>";
		}
	}
	
	
}?>
</div>

I don't get the search results when i click the search button. What is the issue here 

 

Link to comment
https://forums.phpfreaks.com/topic/276636-ajax-search/
Share on other sites

Archived

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

×
×
  • 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.