[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

09/18/2009 at 03:46AM PDT, ID: 24742775
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5

Classic ASP/AJAX auto-complete

Asked by dreamworks_studio in JavaScript, Active Server Pages (ASP), Asynchronous Javascript and XML (AJAX)

Tags: ASP, AJAX, Javascript

Hi all,
I am trying to create an auto-complete which works with a text box, when you start typing in the text box it will display suggestions taken from a database.

I have a solution that is working but there are almost 5000 records in the table and it can take almost a minute for the first set of suggestions to appear, so it is obviously not practical.

Does anyone know of any tweaks I can do to the code to make it a lot quicker (or know of any other solutions), I had thought that limiting the amount of suggestions it shows might help but I'm not sure how to do this. At present if you type "a" then it shows you everything starting with "a" which is hundreds of records.

Many thanks
Gordon
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
ASP to process suggestions:
 
<%
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 rsWords = Server.CreateObject("ADODB.Recordset")
rsWords.ActiveConnection = <connection string>
rsWords.Source = "SELECT medName FROM dbo.prescMeds WHERE (medName LIKE'" + q + "%') ORDER BY medName"
rsWords.CursorType = 2
rsWords.CursorLocation = 2
rsWords.LockType = 3
rsWords.Open()
rsWords_numRows = 5
 
If Not rsWords.EOF Then
	Do While Not rsWords.EOF
		If trim(hint) = "" Then
			hint = "<a href=""javascript:setTextBox('" & rsWords("medName") & "','" & b & "','" & f & "','" & a & "');"">" & rsWords("medName") & "</a>"
		Else
			hint = hint & " <br /><br /> <a href=""javascript:setTextBox('" & rsWords("medName") & "','" & b & "','" & f & "','" & a & "');"">" & rsWords("medName") & "</a>"
		End If
		rsWords.MoveNext()
	Loop
End If
if trim(hint)="" then 
  response.write("no suggestion")
else
  response.write(hint)
end if
 
rsWords.Close()
Set rsWords = Nothing
%>
 
__________________________________________________
 
Javascript/AJAX to process suggestions:
 
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 AJAX!");
  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;
}
 
//this function allows for Clickable suggestions
function setTextBox(thisText,thisBox,thisForm,autoSubmit){
	document.getElementById(thisBox).value = thisText
	//this autoSubmits the form after a suggestion is clicked - it is not working :(
	//if(autoSubmit=='true'){
	//	alert(thisForm);
	//	document.getElementById(thisForm).submit();
	//}
}
 
______________________________________________________________
 
Form and Div to show suggestions:
<form action="prescriptions4.asp" method="post">
      <input type="text" id="txt1" onkeyup="showHint(this.value,'txt1','form1',true)" /> 
</form>
<div id="txtHint"></div>
[+][-]09/18/09 04:13 AM, ID: 25364637

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/18/09 04:26 AM, ID: 25364719

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/18/09 04:38 AM, ID: 25364800

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, Active Server Pages (ASP), Asynchronous Javascript and XML (AJAX)
Tags: ASP, AJAX, Javascript
Sign Up Now!
Solution Provided By: kadaba
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20090824-EE-VQP-74 - Hierarchy / EE_QW_3_20080625