I am new to Ajax but loving it.
Trying to use it for a search tool, so that when you start typing in your keywords, it begins to display answers from the DB.
I have done it, but only after typing once you either click away, or clickaway - and then back on it again.
Code: Select all
<form method='post' action='index.php?page=search&menu=home' name="searchbox" onSubmit="return formCheck(this)">
<input type='text' name='search' class='searchbar' value='' onchange="precheck(this.value)"/>
<input type='submit' value='Search' class='submitsearch' />
</form>Code: Select all
<script>
function precheck(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("srcHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","presearch.php?q="+str,true);
xmlhttp.send();
}
</script>onKeyDown
Many thanks if you read this.