By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Database table data into Javascript?

P: 36
Hi Guys

I am using the following script to pull in 10 UK postcodes into my page:

Expand|Select|Wrap|Line Numbers
  1. <%
  2. sqlStrB = "Select TOP 10 * from postcodetable"
  3.  
  4.   oRsBJ.Open sqlStrB, oDBConn 
  5.   If oRsBJ.eof then
  6. %>
  7. Sorry there are no postcodes
  8. <%
  9.   Else
  10.   %>
  11.  
  12. ---
  13.  
  14. <%
  15.         Do while not oRsBJ.eof
  16.  
  17.         postcode = oRsBJ("postcode") 
  18. %>
  19.  
  20. <%=postcode%>
  21.  
  22. <%
  23.         oRsBJ.MoveNext
  24.         loop 
  25. %>
  26.  
  27. ---
  28.  
  29. <%
  30.   End If
  31.   %>
  32.  
  33. <%
  34. oRsBJ.close
  35. set oRsBJ = nothing
  36. %>
  37.  
  38.  
This works just fine. However, I want to pull in these postcodes into the following peice of javascript:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var locations = [
  3.     'POSTCODE, UK',
  4.     ];
  5. </script>
  6.  
So, if my query returns 3 postcodes then the javascript script would look like:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var locations = [
  3.     'LU4 9LT, UK',
  4.     'HP2 5UY, UK',
  5.     'WD2 6TF, UK',
  6.     ];
  7. </script>
  8.  
How do I insert all the postcode from my query into the javascript in the format above?

Any help would be fully appreciated

Best regards

Rod from the UK
Feb 23 '10 #1
Share this Question
Share on Google+
2 Replies


GazMathias
Expert 100+
P: 189
Did you solve this?

If not simply response.write out your recordset in the loop at that place in the javascript.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. sqlStrB = "Select TOP 10 * from postcodetable"
  3. oRsBJ.Open sqlStrB, oDBConn
  4. %>
  5.  
  6. <script type="text/javascript">
  7. var locations = [
  8.  <% do while not oRsBJ.eof
  9.        response.write "'" & oRsBJ("postcode") & "'," & vbnewline
  10.       oRsBJ.movenext
  11.       Loop %>
  12.     ];
  13. </script>
  14. <%oRsBJ.close
  15. set oRsBJ = nothing%>
  16.  
Mar 1 '10 #2

P: 36
Thanks Gaz.

I will use this in my code.

Thanks for all your help

Best regards

Rod from the UK
Mar 1 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.