15. Write Ajax program to fetch suggestions when is user is
typing in a textbox. (eg like Google suggestions. Hint create array of
suggestions and matching string will be displayed).
Html file :
<html>
<head>
<script
type="text/javascript" >
function m1(str)
{
var ob=false;
ob=new XMLHttpRequest();
ob.open("GET","slip_15.php?q="+str);
ob.send();
ob.onreadystatechange=function()
{
if(ob.readyState==4
&& ob.status==200)
{
document.getElementById("a").innerHTML=ob.responseText;
}
}
}
</script>
</head>
<body>
<form>
Search<input
type=text name=search size="20"
onkeyup="m1(form.search.value)">
<input
type=button value="submit"
onclick="m1(form.search.value)">
</form>
suggestions :<span
id="a"></span><br>
</body>
</html>
Php file :
<?php
$a=array("pune","satara","nashik","sangli","mumbai","murud","akola","dound","dhule","ratnagiri","rajpur");
$q=$_GET['q'];
if(strlen($q)>0)
{
$match="";
for($i=0;$i<count($a);$i++)
{
if(strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if($match=="")
$match=$a[$i];
else
$match=$match.",".$a[$i];
}
}
if($match=="")
echo "No
Suggestios";
else echo $match;
}
?>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.