Classic ASP/AJAX - Auto Suggest Form

Original - ASP Code
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <html>
  3. <head>
  4. <script src="clienthint.js"></script>
  5. </head>
  6. <body>
  7.  
  8. <form>
  9. Enter Word:
  10. <input type="text" id="txt1"
  11. onkeyup="showHint(this.value)">
  12. </form>
  13.  
  14. <p>Suggestions: <span id="txtHint"></span></p>
  15.  
  16. </body>
  17. </html>


JavaScript Code:
  1. var xmlHttp
  2.  
  3. function showHint(str)
  4. {
  5. if (str.length==0)
  6.   {
  7.   document.getElementById("txtHint").innerHTML="";
  8.   return;
  9.   }
  10. xmlHttp=GetXmlHttpObject()
  11. if (xmlHttp==null)
  12.   {
  13.   alert ("Your browser does not support AJAX!");
  14.   return;
  15.   }
  16. var url="gethint.asp";
  17. url=url+"?q="+str;
  18. url=url+"&sid="+Math.random();
  19. xmlHttp.onreadystatechange=stateChanged;
  20. xmlHttp.open("GET",url,true);
  21. xmlHttp.send(null);
  22. }
  23.  
  24. function stateChanged()
  25. {
  26. if (xmlHttp.readyState==4)
  27. {
  28. document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
  29. }
  30. }
  31.  
  32. function GetXmlHttpObject()
  33. {
  34. var xmlHttp=null;
  35. try
  36.   {
  37.   // Firefox, Opera 8.0+, Safari
  38.   xmlHttp=new XMLHttpRequest();
  39.   }
  40. catch (e)
  41.   {
  42.   // Internet Explorer
  43.   try
  44.     {
  45.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  46.     }
  47.   catch (e)
  48.     {
  49.     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  50.     }
  51.   }
  52. return xmlHttp;
  53. }


ASP Code:
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <%
  3. response.expires=-1
  4. Dim rsWords
  5. Dim rsWords_numRows
  6. Dim q
  7. Dim hint
  8. q=ucase(request.querystring("q"))
  9. hint=""
  10. Set rsWords = Server.CreateObject("ADODB.Recordset")
  11. rsWords.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db_hint_words.mdb")
  12. rsWords.Source = "SELECT *  FROM TBL_WORDS  WHERE (word LIKE'" + q + "%') ORDER BY WORD"
  13. rsWords.CursorType = 2
  14. rsWords.CursorLocation = 2
  15. rsWords.LockType = 3
  16. rsWords.Open()
  17. rsWords_numRows = 0
  18.  
  19. If Not rsWords.EOF Then
  20.     Do While Not rsWords.EOF
  21.         If trim(hint) = "" Then
  22.             hint = rsWords("word")
  23.         Else
  24.             hint = hint & " , " & rsWords("word")
  25.         End If
  26.         rsWords.MoveNext()
  27.     Loop
  28. End If
  29. if trim(hint)="" then
  30.   response.write("no suggestion")
  31. else
  32.   response.write(hint)
  33. end if
  34.  
  35. rsWords.Close()
  36. Set rsWords = Nothing
  37. %>



from: http://forums.aspfree.com/code-bank-54/auto-suggest-form-199141.html
posted @ 2009-10-15 09:39  ®Geovin Du Dream Park™  阅读(398)  评论(0编辑  收藏  举报