Solved

Autocomplete jQuery not working in Classic ASP page

Posted on 2013-12-18
6
957 Views
Last Modified: 2013-12-19
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

0
Comment
Question by:capturetheflag
  • 3
  • 2
6 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
can you please post the generated HTML of your page?

Thanks.
Rainer
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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

0
 
LVL 1

Author Comment

by:capturetheflag
Comment Utility
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

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
Comment Utility
If it is working, it looks fine to me.  

The only thing I would do differently is place is use getrows and throw all the data to an array.

'create recordset
MyArray=rs.getrows()
rs.close
'notice you can close the rs here then use the array throughout your code without accessing the db
for r = LBound(MyArray, 2) To UBound(MyArray, 2)
    	ID= MyArray(0, r)
	First = MyArray(1, r)
	Last = MyArray(2, r)



      response.write "ID= "&ID&"<br>"
next

Open in new window

I have more detail on this here http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_28201280.html#a39376842

If you want to get this going, maybe ask in a new thread.
0
 
LVL 1

Author Closing Comment

by:capturetheflag
Comment Utility
Thanks for the help Padas.  I am opening a new thread about auto complete and hitting a 100 thousand user name db.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now