ajaxfunction.js
autocomp.asp
var xmlhttp = false; //Check if we are using IE. try { //If the javascript version is greater than 5. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { //If not, then use the older active x object. try { //If we are using IE. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { //Else we must be using a non-IE browser. xmlhttp = false; } } //If we are using a non-IE browser, create a JavaScript instance of the object. if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); } function autocomplete (thevalue, e){ theObject = document.getElementById("autocmpl"); theObject.style.visibility = "visible"; theObject.style.width = "152px"; var posx = 0; var posy = 0; posx = (findPosX (document.getElementById("fname")) + 1); posy = (findPosY (document.getElementById("fname")) + 23); theObject.style.left = posx + "px"; theObject.style.top = posy + "px"; theObject.style.position="absolute"; var theextrachar = e.which; if (theextrachar == undefined){ theextrachar = e.keyCode; } //The location we are loading the page into. var objID = "autocmpl"; //Take into account the backspace. if (theextrachar == 8){ if (thevalue.length == 1){ var serverPage = "autocomp.asp"; } else { var serverPage = "autocomp.asp" + "?sstring=" + thevalue.substr (0, (thevalue.length -1)); } } else { var serverPage = "autocomp.asp" + "?sstring=" + thevalue + String.fromCharCode (theextrachar); } var obj = document.getElementById(objID); xmlhttp.open("GET", serverPage); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { obj.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); } function findPosX(obj){ var curleft = 0; if (obj.offsetParent){ while (obj.offsetParent){ curleft += obj.offsetLeft obj = obj.offsetParent; } } else if (obj.x){ curleft += obj.x; } return curleft; } function findPosY(obj){ var curtop = 0; if (obj.offsetParent){ while (obj.offsetParent){ curtop += obj.offsetTop obj = obj.offsetParent; } } else if (obj.y){ curtop += obj.y; } return curtop; } function setvalue (thevalue){ acObject = document.getElementById("autocmpl"); acObject.style.visibility = "hidden"; acObject.style.height = "0px"; acObject.style.width = "0px"; document.getElementById("fname").value = thevalue; }
autocomp.asp
<!--#include file="conn.asp"--> <% chkchar=Request.QueryString("sstring") if (chkchar <> "")then sql1="select distinct fname from usersTbl where fname like'"&chkchar&"%'" rs1=dbconn.execute(sql1) Response.Write("<div style='background: #CCCCCC; border-style: solid; border-width: 1px; border-color: #000000;'>") cunt=0 while not rs1.EOF And cunt<5 cunt=cunt+1 %> <div style="padding: 4px; height: 14px;" onmouseover="this.style.background = '#EEEEEE'" onmouseout="this.style.background = '#CCCCCC'" onclick="setvalue ('<%=rs1("fname")%>')"><%=rs1("fname")%></div> <% rs1.MoveNext wend Response.Write("</div>") end if %>userlogin.html
<script type="text/javascript" src="ajaxfunction.js"></script> <form name="myform" method="post" action="process.asp"> First Name :<input type="text" id="fname" name="fname" onkeypress="autocomplete(this.value, event)"> </form> <div id="autocmpl"></div>