Database table data into Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rjoseph
    New Member
    • Sep 2007
    • 36

    Database table data into Javascript?

    Hi Guys

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

    Code:
    <%
    sqlStrB = "Select TOP 10 * from postcodetable"
    
      oRsBJ.Open sqlStrB, oDBConn 
      If oRsBJ.eof then
    %>
    Sorry there are no postcodes
    <%
      Else
      %>
    
    ---
    
    <%
            Do while not oRsBJ.eof
    
            postcode = oRsBJ("postcode") 
    %>
    
    <%=postcode%>
    
    <%
            oRsBJ.MoveNext
            loop 
    %>
    
    ---
    
    <%
      End If
      %>
    
    <%
    oRsBJ.close
    set oRsBJ = nothing
    %>
    This works just fine. However, I want to pull in these postcodes into the following peice of javascript:

    Code:
    <script type="text/javascript">
    var locations = [
        'POSTCODE, UK',
        ];
    </script>
    So, if my query returns 3 postcodes then the javascript script would look like:

    Code:
    <script type="text/javascript">
    var locations = [
        'LU4 9LT, UK',
        'HP2 5UY, UK',
        'WD2 6TF, UK',
        ];
    </script>
    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
  • GazMathias
    Recognized Expert New Member
    • Oct 2008
    • 228

    #2
    Did you solve this?

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

    Code:
    <%
    sqlStrB = "Select TOP 10 * from postcodetable"
    oRsBJ.Open sqlStrB, oDBConn
    %>
    
    <script type="text/javascript">
    var locations = [
     <% do while not oRsBJ.eof
           response.write "'" & oRsBJ("postcode") & "'," & vbnewline
          oRsBJ.movenext
          Loop %>
        ];
    </script>
    <%oRsBJ.close
    set oRsBJ = nothing%>
    Last edited by GazMathias; Mar 1 '10, 12:59 PM. Reason: accidentally posted incomplete!

    Comment

    • rjoseph
      New Member
      • Sep 2007
      • 36

      #3
      Thanks Gaz.

      I will use this in my code.

      Thanks for all your help

      Best regards

      Rod from the UK

      Comment

      Working...