----------------------------------------------------------------------------------------------------------
function autocomplete(thevalue, e)
{
theObject = document.getElementById("autocompletediv");
theObject.style.visiblity = "visible";
theObject.style.width = "152px";
var posx = 0;
var posy = 0;
posx = (findPosX (document.getElementById("city")) + 1);
posy = (findPosY (document.getElementById("city")) + 23);
theObject.style.left = posx + "px";
theObject.style.top = posy + "px";
var theextrachar = e.which;
if(theextrachar == "undefined")
{ theextrachar = e.keyCode; }
var objID = "autocompletediv";
//take into account the backspace
if(theextrachar ==
{ if(thevalue.length == 1){
var serverpage = "list_cities.php";}
else{
var serverpage = "list_cities.php" + "?sstring=" + thevalue.substr(0,(thevalue.length -1)); }}
else {
var serverpage = "list_cities.php" + "?sstring=" + thevalue + String.fromCharCode(theextrachar);}
var objXML = createXMLHttpRequest();
var obj = document.getElementById(objID);
obj.style.visibility = "visible";
objXML.open("GET", serverpage);
objXML.onreadystatechange = function()
{
if (objXML.readyState == 4 && objXML.status == 200)
{obj.innerHTML = objXML.responseText;}
}
objXML.send(null);
}
function setvalue (thevalue)
{
acObject = document.getElementById("autocompletediv");
acObject.style.visibility = "hidden";
acObject.style.height = "0px";
acObject.style.width = "0px";
document.getElementById("city").value = thevalue;
}
----------------------------------------------------------------------------------------------------------
the above is my javascript code
----------------------------------------------------------------------------------------------------------
<div>Enter text here<INPUT type="text" name="clocation" size="27" onkeypress="autocomplete(this.value, event)" id="city"></div>
<div id="autocompletediv" class="autocomp"></div>
----------------------------------------------------------------------------------------------------------
this is my html code for calling the javascript function
----------------------------------------------------------------------------------------------------------
and the below is the php code
----------------------------------------------------------------------------------------------------------
<?php
$names = array('anil','anoop','bhuvan','bhavana','chandu','chinna','deepa','deepak','funny','gauri','hari','imran','ismail','jim','kiran','lavanya','maney','naveen','oswin','praveen','qaran','ravi','satish','teena','uma','varun','yasir','zaheer');
$foundarr = array ();
//Go through the names array and load any matches into the foundarr array.
if ($_GET['sstring'] != "")
{
for ($i = 0; $i < count ($names); $i++)
{
if (substr_count (strtolower ($names[$i]), strtolower ($_GET['sstring'])) > 0)
{
$foundarr[] = $names[$i];
}}}
//If we have any matches.
if (count ($foundarr) > 0)
{
//Then display them.
?>
<div style="background: #FFFFFF; border-style: solid; border-width: 1px; border-color: #000000;">
<?php for ($i = 0; $i < count ($foundarr); $i++){?>
<div style="padding:4px; height:14px;" onmouseover="this.style.background = '#FF0033'" onmouseout="this.style.background = '#FFFFFF'" onclick="setvalue ('<?php echo $foundarr[$i];?>')">
<?php echo $foundarr[$i]; ?>
</div>
<?php } ?>
</div>
<?php }?>
----------------------------------------------------------------------------------------------------------