Search code examples
javascripthtmlajaxautocompleteautosuggest

Somehow attaching Autocomplete Div with the entry textbox


  • IIS Version: IIS 8.5
    • Server: Windows Server 2012 R2
    • Root folder: D:\Sites\Default\WWWRoot
    • Site Directory: D:\Sites\Default\WWWRoot\Website
    • Languages: ASP, JavaScript, HTML, AJAX

So I have built this page that has a search box. When you enter a name in the search box, it uses ajax to connect to another page, use the search criteria to query our database, and returns "like" results for the search entry in a div box in real-time (and thankfully its a fast return). So I have this working. Here is my problem. The Div itself needs to be attached to the search box so it acts like, for lack of a better example, Google's search box that when you type something, the auto suggest behaves like a dropdown for the search field. I have done some research, and it looks like HTML5 has a built-in tag that allows this. Unfortunately, I am not using HTML5. Any help would be appreciated. All I want to do is make the div act like a "dropdown" from the search field once it returns results that you could scroll through with your keyboard if you wanted. I also attached a screenshot of how it looks now (Left side of image) and an example of how I would like it to function (right side of the image)Example

Thanks in advance for your help!

Here is the code for the JavaScript/HTML (Main Page):

 <script>
    var xmlHttp
    function showHint(str, box, thisForm, autoSubmit)
    {
     if (str.length==0)
     { 
      document.getElementById("txtHint").innerHTML="";
      return;
     }

    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
      {
        alert ("Your browser does not support this feature. Please search manually.");
        return;
      } 

    var url="gethint.asp";
    url=url+"?q="+str;
    url=url+"&b="+box;
    url=url+"&f="+thisForm;
    url=url+"&a="+autoSubmit;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

    } 


function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    { 
        document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
    }
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
  {

  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }

catch (e)
  {
    // Internet Explorer
    try
    {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
      catch (e)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }

return xmlHttp;

}


</script>

<font face="calibri">
<form action="" method="post">
      <input type="text" id="txt1" onkeyup="showHint(this.value,'txt1','form1',true)" autocomplete="off" style="width: 250px;"/> 
      <div id="txtHint"></div>
</form>
</font>

And here is the code for the page it listens for to query:

    <!--#include file="Databases.asp"-->
<%

response.expires=-1

Dim rsWords

Dim rsWords_numRows

Dim q

Dim b

Dim hint

q=ucase(request.querystring("q"))

b=(request.querystring("b"))

f=(request.querystring("f"))

a=(request.querystring("a"))

hint=""




Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open = "Provider=SQLOLEDB;Data Source=" & DatabaseServerR & ";Integrated Security=SSPI;Network Library=DBMSSOCN;Initial Catalog=" & Database_R & ";"
    Set rsWords = Conn.Execute ("SELECT TOP 20 WKR_FLL_NM, WDW_LGON_ID FROM dbo.HPeepzs With(NoLock) WHERE WKR_FLL_NM LIKE'" + q + "%' OR WDW_LGON_ID LIKE'" + q + "%' OR WKR_ID LIKE'" + q + "%' ORDER BY WKR_FLL_NM ASC")





If Not rsWords.EOF Then

'If entrytype = 1 Then
    'Do While Not rsWords.EOF
    '   If trim(hint) = "" Then
    '       hint = "<a href=""javascript:setTextBox('" & rsWords("PC_Name") & "','" & b & "','" & f & "','" & a & "');"">" & rsWords("PC_Name") & "</a>"
    '   Else
    '       hint = hint & "<a href=""javascript:setTextBox('" & rsWords("PC_Name") & "','" & b & "','" & f & "','" & a & "');"">" & rsWords("PC_Name") & "</a>"
    '   End If
    '   rsWords.MoveNext()
    'Loop
'Else
    Do While Not rsWords.EOF
        If trim(hint) = "" Then
            hint = "<li> <a href=""javascript:setTextBox('" & rsWords("WDW_LGON_ID") & "','" & b & "','" & f & "','" & a & "');"">" & rsWords("WKR_FLL_NM") & " (" & rsWords("WDW_LGON_ID") & ")</a>"
        Else
            hint = hint & "<li> <a href=""javascript:setTextBox('" & rsWords("WDW_LGON_ID") & "','" & b & "','" & f & "','" & a & "');"">" & rsWords("WKR_FLL_NM") & " (" & rsWords("WDW_LGON_ID") & ") </a>"
        End If
        rsWords.MoveNext()
    Loop
'End If

End If

if trim(hint)="" then 

  response.write("Unable To Find Your Search")

else

  response.write(hint)

end if


rsWords.Close()
Conn.Close

Set rsWords = Nothing
Set Conn = Nothing
%>

Solution

  • So, I have figured out what to do. I just placed the div underneath the search field, and it acts as a popup dropdown that pops up when search is entered by using margin style

    <div id="txtHint" style="position:absolute;margin-top:2px;margin-left:0px;z-index: 99 !important;"></div>