source.html
<html>
<head>
<script src="sourcehint.js"></script>
</head>
<body>
<form>
First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
sourcehint.js
var xmlhttp
function showHint(str){
if (str.length==0){
document.getElementById("txtHint").innerHTML="";
return;
}
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null){
alert ("Your browser does not support XMLHTTP!");
return;
}
var url="gethint.php";
url=url+"?q=" + str;
url=url+"&sid=" + Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
function stateChanged(){
if (xmlhttp.readyState==4){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject(){
if (window.XMLHttpRequest){
return new XMLHttpRequest();
}
if (window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
gethint.php
<?php
$a[]="Ravi";
$a[]="Sachin";
$a[]="Sandeep";
$a[]="Varun";
$a[]="Avneesh";
$a[]="Mukul";
$q=$_GET["q"];
if (strlen($q) > 0){
$hint="";
for($i=0; $i<count($a); $i++){
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))){
if ($hint==""){
$hint=$a[$i];
}
else{
$hint=$hint." , ".$a[$i];
}
}
}
}
if ($hint == ""){
$response="no suggestion";
}
else{
$response=$hint;
}
echo $response;
?>
|