Link to home
Start Free TrialLog in
Avatar of capturetheflag
capturetheflag

asked on

Autocomplete jQuery not working in Classic ASP page

Hello,

I am trying to get jQuery auto-complete to work in a classic ASP web page.
But is not working
Thanks for the help.
<%dim output, outstr%>

 <%if not rs3.eof then
		'output = ""
		do while not rs3.eof%>     
	
    <%'response.write rs3("first_name") & " " & rs3("middle_name") & " " & " " & rs3("last_name") & " " & rs3("user_id") & "<BR>"
	
	output = rs3("first_name")
	outstr = """" & output & """," 
	response.write outstr %> 

	<%rs3.movenext
		loop
end if		%>



       <script type="text/javascript">
	   $(function() {	   
		   var availableTags = [<%= outstr%>]; 
		   $( "#username" ).autocomplete({
			   source: availableTags
		   });
	   });
</script>

Open in new window

Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Hi,
can you please post the generated HTML of your page?

Thanks.
Rainer
The problem is your trying to mix server side code and client side code.  Your asp/vb is serverside code and runs on the server BEFORE the html is written to the browser.  Javascript and jquery are client side languages and run AFTER the html and css loads.  

What you need to do is look at the sample code http://jqueryui.com/autocomplete/ and generate the html from your asp.

The link I gave you has this sample
$(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });

Open in new window


What you would do in your asp code is this

    <% 
dim theChoices
theChoices=""
do until rs3.eof
theChoices=theChoices&"""&rs3("first_name")&"""
rs3.movenext
if not rs3.eof then
   theChoices=theChoices&"," ' only add a comma if more data
end if
loop
' You should now have a file looking like:  "Bog","Dan","Pete", "Roger"

$(function() {
    var availableTags = [
     <%=theChoices%>
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });

Open in new window

Avatar of capturetheflag
capturetheflag

ASKER

Hello Padas,
Thanks for your help. Yes, I have looked at the jQuery site for auto complete.
I have it working now.  Here is my code.  Please let me know if the code could be cleaned up in any way. Thanks again.
<%
dim output, outstr, mystring%>
<%
if not rs3.eof then
	do while not rs3.eof
			theChoices=theChoices&""&rs3("first_name")&""","""
		rs3.movenext
	loop
end if		

'''''cleanup and reformat string
theChoices = Replace(theChoices,""","""",""","")
theChoices = """"&theChoices &""""
'response.write theChoices
%>
<script type="text/javascript">
   $(function() {	   
	   var availableTags = [<%=theChoices%>]; 
	   $( "#username" ).autocomplete({
		   source: availableTags
	   });
   });
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help Padas.  I am opening a new thread about auto complete and hitting a 100 thousand user name db.
Ok, please post the link to the question here.  

In short, on the asp page that generates the 100K names, you want to make the query only trigger after x amount of characters have been typed.  Typically I would wait until at least 3.  Then in your query you probably want to limit the total to 50 or so.

Your sql might look like:
theName=request("name")
"Select top 50 firstName+' '+lastName as FullName from mytable where firstName like '"&theName&"%'

Open in new window