0% found this document useful (0 votes)
33 views3 pages

Auto Complete

This document describes a program that allows searching through an array of names and auto-completing the search results as the user types in an input field. The program uses AJAX to call a PHP file that searches the names array and returns any matches to populate the results div.

Uploaded by

maruthi631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Auto Complete

This document describes a program that allows searching through an array of names and auto-completing the search results as the user types in an input field. The program uses AJAX to call a PHP file that searches the names array and returns any matches to populate the results div.

Uploaded by

maruthi631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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