You are on page 1of 3

AUTO-COMPLETE: searching names from an array.

Program: Sample.php
<head>
<script>
var x;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
x=new XMLHttpRequest();
} else { // code for IE6, IE5
x=new ActiveXObject("Microsoft.XMLHTTP");
}
function disp(w)
{ var sp;
//alert(w);
var d=document.getElementById("show");
if(w.length==1){
sp="fill.php"+"?v="+w;
}
else{
var sp="fill.php"+"?v="+"";
}
x.onreadystatechange=function()
{
if(x.readyState==4 && x.status==200)
{
d.innerHTML=x.responseText;
}
}
x.open("GET",sp);
x.send(null);

function setvalue (thevalue){


acObject = document.getElementById("show");
acObject.style.visibility = "hidden";
acObject.style.height = "0px";
acObject.style.width = "0px";
document.getElementById("user").value = thevalue;
}

</script>
</head>
<body>
<form>
<h1>search for students names</h1><input type="text" name="user" id="user"
onkeyup=disp(this.value)>
</form>
<div id="show"></div>
</body>
</html>
Program:fill.php

<?php
$v=$_GET['v'];
$names = array ("Lee Babin","Joe Smith","John Doe");
$foundarr = array ();

if ($_GET['v']!= " ")


{
//echo $v;
for ($i = 0; $i < count ($names); $i++)
{
//echo substr_count (strtolower ($names[$i]),strtolower ($v)
if (substr_count (strtolower ($names[$i]),strtolower ($v)) > 0)
{
$foundarr[] = $names[$i];

}
}
else{
echo"enter value for search";
}
if(count($foundarr)>0)
{
?>
<div>
<?php
for ($i = 0; $i < count ($foundarr); $i++){
?>
<div style="padding: 4px; height: 14px;" onclick="setvalue('<?php echo
$foundarr[$i]; ?>')">
<?php echo $foundarr[$i]; ?>
</div>
<?php
}
?>
</div>
<?php
}
else{
echo"<h1 style=\"color:red\">not found give correct value";
}
?>

You might also like