What programming language are you using on the server side? I think you'll have more help in the appropriate forum for that language.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
The JavaScript in the page should do that:
<html>
<head>
<script type="text/javascript">
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
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("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.asp?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
</html>
The input field has an onkeyup event handler attached to it:
<input type="text" size="30" onkeyup="showResult(this.value)">
This will fire the showResults() function every time a key is pressed while focus is on the textbox (writing in the textbox).
showResults() will then grab the PHP page from the server and display the results in
<div id="livesearch"></div>
Do you have Firefox? When working with Javascript it is good to have Firefox and Firebug. Firebug is about the best (browser based) JavaScript debugger out there.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
It would really help you if you looked into XMLHttpRequest. There are a lot of tutorials and examples online to guide you. It is the basis of "AJAX".
xmlhttp.open("GET","livesearch.asp?q="+str,true);
That just opens a HTTP connection to the URL given: livesearch.asp?q="+str
The q is the HTTP query parameter that holds the search you typed in. This is what is passed to PHP, and PHP should base it's response on.
xmlhttp.send();
Will send the HTTP request through the HTTP connection opened.
The
xmlhttp.onreadystatechang
e is a function that will be called, then the HTTP response/reply changes state.
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
The above code listens for the HTTP response, and when it receives the full response with a successful HTTP status (200) it writes the HTML received in the HTTP request to the div with id="livesearch".
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101