Using ASP Variables

Discussion in 'Javascript' started by Craig L, Nov 4, 2003.

  1. Craig L

    Craig L Guest

    I use ASP to obtain data from a database and I have a piece of javascript
    code I use on my website. I want to use the database variables in my
    javascript. I'm not very familiar with javascript, but here is some info
    similar to what I'm trying to do.

    <%
    strFirstName=session("FirstName") ASP code
    strLastName=session("LastName")
    %>

    The javascript code I want to use the variables in.
    Items[1]=["First Name", strFirstName,""]
    Items[2]=["Last Name", strLastName,""]

    Please help or point me to a website with this solution. Thanks much for
    your help.

    Craig

    (remove SPAM before sending)
     
    Craig L, Nov 4, 2003
    #1
    1. Advertising

  2. Craig L

    bengee Guest

    Craig L wrote:
    > I use ASP to obtain data from a database and I have a piece of javascript
    > code I use on my website. I want to use the database variables in my
    > javascript. I'm not very familiar with javascript, but here is some info
    > similar to what I'm trying to do.
    >
    > <%
    > strFirstName=session("FirstName") ASP code
    > strLastName=session("LastName")
    > %>
    >
    > The javascript code I want to use the variables in.
    > Items[1]=["First Name", strFirstName,""]
    > Items[2]=["Last Name", strLastName,""]


    You can't access the ASP variables directly, because ASP is done on the
    server, and Javascript is on the client (web browser). By the time the
    Javascript is "executing" the ASP has already finished.

    You can try this though :-

    Items[1]=["First Name", "<%=strFirstName%>",""]
    Items[2]=["Last Name", "<%=strLastName%>",""]

    HTH!

    bengee
     
    bengee, Nov 4, 2003
    #2
    1. Advertising

  3. Craig L

    Craig L Guest

    Bengee:
    Thanks for the help. the <%= info didn't help. Do you know if the javascript
    code can use it if I create a session object for the variable? I tried some
    of this with no luck.

    Craig

    "bengee" <> wrote in message
    news:L8Tpb.4373$9.net...
    > Craig L wrote:
    > > I use ASP to obtain data from a database and I have a piece of

    javascript
    > > code I use on my website. I want to use the database variables in my
    > > javascript. I'm not very familiar with javascript, but here is some info
    > > similar to what I'm trying to do.
    > >
    > > <%
    > > strFirstName=session("FirstName") ASP code
    > > strLastName=session("LastName")
    > > %>
    > >
    > > The javascript code I want to use the variables in.
    > > Items[1]=["First Name", strFirstName,""]
    > > Items[2]=["Last Name", strLastName,""]

    >
    > You can't access the ASP variables directly, because ASP is done on the
    > server, and Javascript is on the client (web browser). By the time the
    > Javascript is "executing" the ASP has already finished.
    >
    > You can try this though :-
    >
    > Items[1]=["First Name", "<%=strFirstName%>",""]
    > Items[2]=["Last Name", "<%=strLastName%>",""]
    >
    > HTH!
    >
    > bengee
    >
     
    Craig L, Nov 4, 2003
    #3
  4. Craig L

    bengee Guest

    Craig L wrote:
    > Bengee:
    > Thanks for the help. the <%= info didn't help. Do you know if the javascript
    > code can use it if I create a session object for the variable? I tried some
    > of this with no luck.


    There's no way Javascript can access an ASP variable. However,
    Javascript is the "output" from the ASP code, so you can set a
    Javascript variable to be that of an ASP variable. Once this is set
    though and being viewed in the browser, you can't change it server-side
    without some sort of re-sumbit mechanism.

    Have a look a Mike's example below.

    bengee
     
    bengee, Nov 4, 2003
    #4
  5. Craig L

    Craig L Guest

    Bengee:
    I'm trying what Mike mentioned since it really makes sense; getting ASP to
    generate the javascript code, but it doesn't work for me yet. I have the
    response.buffer=True set. Not sure what I'm doing wrong.

    Craig

    "bengee" <> wrote in message
    news:tHUpb.4415$9.net...
    > Craig L wrote:
    > > Bengee:
    > > Thanks for the help. the <%= info didn't help. Do you know if the

    javascript
    > > code can use it if I create a session object for the variable? I tried

    some
    > > of this with no luck.

    >
    > There's no way Javascript can access an ASP variable. However,
    > Javascript is the "output" from the ASP code, so you can set a
    > Javascript variable to be that of an ASP variable. Once this is set
    > though and being viewed in the browser, you can't change it server-side
    > without some sort of re-sumbit mechanism.
    >
    > Have a look a Mike's example below.
    >
    > bengee
    >
     
    Craig L, Nov 4, 2003
    #5
  6. Craig L

    bengee Guest

    Craig L wrote:
    > Bengee:
    > I'm trying what Mike mentioned since it really makes sense; getting ASP to
    > generate the javascript code, but it doesn't work for me yet. I have the
    > response.buffer=True set. Not sure what I'm doing wrong.


    Post some code here then and let us have a look.
     
    bengee, Nov 4, 2003
    #6
  7. Craig L

    Jeff North Guest

    On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
    <> wrote:

    >| Here is my exact code. This page receives a URL as the following
    >| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
    >|
    >| In ASP, a reference to Request("TBLS") will return the string
    >| "AttMarket,ConfComment"
    >|
    >| I take that info and save it into a JavaScript array that I can access
    >| throughout the page on the client side...
    >| <preliminary HTML stuff>
    >| <body onload="Loaded()">
    >| <%
    >|
    >| '-- A bunch of code that means nothing as far as your question goes...
    >|
    >| '-- Throw out some JavaScript make it easy to get to the Request
    >| parameters.
    >| Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34) & ">"
    >| Response.Write "numTables=" & Request("TBLS").Count & ";"
    >| Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1) &
    >| ");"
    >| For i = 1 to Request("TBLS").Count
    >| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
    >| Request("TBLS")(i) & chr(34) & ";"
    >| Next
    >| Response.Write "</script>"
    >|
    >| '-- More code that means nothing
    >|
    >| %>
    >| </body>


    You are trying to get the javascript to read from the database. This
    will not work. You need to do this server-side.

    Have a look at
    http://www.asp101.com/samples/viewasp.asp?file=db_dsn.asp


    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Nov 5, 2003
    #7
  8. Craig L

    Grant Wagner Guest

    Craig L wrote:

    > I use ASP to obtain data from a database and I have a piece of javascript
    > code I use on my website. I want to use the database variables in my
    > javascript. I'm not very familiar with javascript, but here is some info
    > similar to what I'm trying to do.
    >
    > <%
    > strFirstName=session("FirstName") ASP code
    > strLastName=session("LastName")
    > %>
    >
    > The javascript code I want to use the variables in.
    > Items[1]=["First Name", strFirstName,""]
    > Items[2]=["Last Name", strLastName,""]
    >
    > Please help or point me to a website with this solution. Thanks much for
    > your help.
    >
    > Craig
    >
    > (remove SPAM before sending)


    Trivial:

    Items[1]=["First Name", "<%= session('FirstName') %>",""];
    Items[2]=["Last Name", "<%= session('LastName') %>",""];

    If <%= %> doesn't evaluate properly inside double-quotes (I'm not sure how
    ASP deals with this), then you could use something like:

    Items[1]=["First Name", <%= dquote(session("FirstName")) %>,""];
    Items[2]=["Last Name", <%= dquote(session("LastName")) %>,""];

    (where dquote() is a little function that you provide that wraps the passed
    string in double-quotes)

    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Nov 5, 2003
    #8
  9. Craig L

    Jeff North Guest

    On Wed, 5 Nov 2003 08:50:38 -0600, in comp.lang.javascript "Mike"
    <> wrote:

    >| YES, it will work, and it DOES work....


    Sorry, I should've replied to Graig L posting.
    But my point still stands. You can not use client-side javascript to
    read data directly from a database.

    >| "Jeff North" <> wrote in message
    >| news:...
    >| > On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
    >| > <> wrote:
    >| >
    >| > >| Here is my exact code. This page receives a URL as the following
    >| > >| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
    >| > >|
    >| > >| In ASP, a reference to Request("TBLS") will return the string
    >| > >| "AttMarket,ConfComment"
    >| > >|
    >| > >| I take that info and save it into a JavaScript array that I can access
    >| > >| throughout the page on the client side...
    >| > >| <preliminary HTML stuff>
    >| > >| <body onload="Loaded()">
    >| > >| <%
    >| > >|
    >| > >| '-- A bunch of code that means nothing as far as your question goes...
    >| > >|
    >| > >| '-- Throw out some JavaScript make it easy to get to the Request
    >| > >| parameters.
    >| > >| Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34)
    >| & ">"
    >| > >| Response.Write "numTables=" & Request("TBLS").Count & ";"
    >| > >| Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1)
    >| &
    >| > >| ");"
    >| > >| For i = 1 to Request("TBLS").Count
    >| > >| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
    >| > >| Request("TBLS")(i) & chr(34) & ";"
    >| > >| Next
    >| > >| Response.Write "</script>"
    >| > >|
    >| > >| '-- More code that means nothing
    >| > >|
    >| > >| %>
    >| > >| </body>
    >| >
    >| > You are trying to get the javascript to read from the database. This
    >| > will not work. You need to do this server-side.
    >| >
    >| > Have a look at
    >| > http://www.asp101.com/samples/viewasp.asp?file=db_dsn.asp
    >| >
    >| >
    >| > ---------------------------------------------------------------
    >| > : Remove your pants to reply
    >| > ---------------------------------------------------------------
    >|


    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Nov 5, 2003
    #9
  10. Craig L

    Craig L Guest

    Hey Gang:
    I'll try what Jeff said to do as soon as I get a chance - probably this
    afternoon. Will let you know and post the working code for others.

    Craig

    "Jeff North" <> wrote in message
    news:...
    > On Wed, 5 Nov 2003 08:50:38 -0600, in comp.lang.javascript "Mike"
    > <> wrote:
    >
    > >| YES, it will work, and it DOES work....

    >
    > Sorry, I should've replied to Graig L posting.
    > But my point still stands. You can not use client-side javascript to
    > read data directly from a database.
    >
    > >| "Jeff North" <> wrote in message
    > >| news:...
    > >| > On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
    > >| > <> wrote:
    > >| >
    > >| > >| Here is my exact code. This page receives a URL as the following
    > >| > >| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
    > >| > >|
    > >| > >| In ASP, a reference to Request("TBLS") will return the string
    > >| > >| "AttMarket,ConfComment"
    > >| > >|
    > >| > >| I take that info and save it into a JavaScript array that I can

    access
    > >| > >| throughout the page on the client side...
    > >| > >| <preliminary HTML stuff>
    > >| > >| <body onload="Loaded()">
    > >| > >| <%
    > >| > >|
    > >| > >| '-- A bunch of code that means nothing as far as your question

    goes...
    > >| > >|
    > >| > >| '-- Throw out some JavaScript make it easy to get to the Request
    > >| > >| parameters.
    > >| > >| Response.Write "<script Language=" & chr(34) & "JavaScript" &

    chr(34)
    > >| & ">"
    > >| > >| Response.Write "numTables=" & Request("TBLS").Count & ";"
    > >| > >| Response.Write "allTables = new Array(" &

    (Request("TBLS").Count - 1)
    > >| &
    > >| > >| ");"
    > >| > >| For i = 1 to Request("TBLS").Count
    > >| > >| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
    > >| > >| Request("TBLS")(i) & chr(34) & ";"
    > >| > >| Next
    > >| > >| Response.Write "</script>"
    > >| > >|
    > >| > >| '-- More code that means nothing
    > >| > >|
    > >| > >| %>
    > >| > >| </body>
    > >| >
    > >| > You are trying to get the javascript to read from the database. This
    > >| > will not work. You need to do this server-side.
    > >| >
    > >| > Have a look at
    > >| > http://www.asp101.com/samples/viewasp.asp?file=db_dsn.asp
    > >| >
    > >| >
    > >| > ---------------------------------------------------------------
    > >| > : Remove your pants to reply
    > >| > ---------------------------------------------------------------
    > >|

    >
    > ---------------------------------------------------------------
    > : Remove your pants to reply
    > ---------------------------------------------------------------
     
    Craig L, Nov 6, 2003
    #10
  11. Craig L

    Craig L Guest

    I think I can get this to a simplier problem now that I have played with it.
    Here is what is happening. This code works for what I'm trying to do:

    <SCRIPT language=JavaScript>

    var strFirstName="Don"

    var strLastName="Rose"

    </SCRIPT>

    <SCRIPT>

    Items[0]=[strFirstName, "", ""];

    Items[1]=[strLastName, "", ""];

    </SCRIPT>

    This code getting ASP to write the javascript equivalent like this, doesn't
    work:

    <%

    response.write "<SCRIPT Language=" & chr(34) & "JavaScript" & chr(34)

    response.write "var strFirstName=" & chr(34) & "Don" & chr(34)

    response.write "var strLastName=" & chr(34) & "Rose" & chr(34)

    response.write "</SCRIPT>"

    %>

    <SCRIPT>

    Items[0]=[strFirstName, "", ""];

    Items[1]=[strLastName, "", ""];

    </SCRIPT>

    If I can just get this right, I can get the rest working on my own.

    Craig

    "Grant Wagner" <> wrote in message
    news:...

    > Craig L wrote:
    >
    > > I use ASP to obtain data from a database and I have a piece of

    javascript
    > > code I use on my website. I want to use the database variables in my
    > > javascript. I'm not very familiar with javascript, but here is some info
    > > similar to what I'm trying to do.
    > >
    > > <%
    > > strFirstName=session("FirstName") ASP code
    > > strLastName=session("LastName")
    > > %>
    > >
    > > The javascript code I want to use the variables in.
    > > Items[1]=["First Name", strFirstName,""]
    > > Items[2]=["Last Name", strLastName,""]
    > >
    > > Please help or point me to a website with this solution. Thanks much for
    > > your help.
    > >
    > > Craig
    > >
    > > (remove SPAM before sending)

    >
    > Trivial:
    >
    > Items[1]=["First Name", "<%= session('FirstName') %>",""];
    > Items[2]=["Last Name", "<%= session('LastName') %>",""];
    >
    > If <%= %> doesn't evaluate properly inside double-quotes (I'm not sure how
    > ASP deals with this), then you could use something like:
    >
    > Items[1]=["First Name", <%= dquote(session("FirstName")) %>,""];
    > Items[2]=["Last Name", <%= dquote(session("LastName")) %>,""];
    >
    > (where dquote() is a little function that you provide that wraps the

    passed
    > string in double-quotes)
    >
    > --
    > | Grant Wagner <>
    >
    > * Client-side Javascript and Netscape 4 DOM Reference available at:
    > *
    >

    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
    >
    > * Internet Explorer DOM Reference available at:
    > *
    >

    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
    >
    > * Netscape 6/7 DOM Reference available at:
    > * http://www.mozilla.org/docs/dom/domref/
    > * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    > * http://www.mozilla.org/docs/web-developer/upgrade_2.html
    >
    >
     
    Craig L, Nov 7, 2003
    #11
  12. Craig L

    Craig L Guest

    Visit the posting "ANS: Using ASP Variables" posted on 11/09/03 for the
    solution to this issue.

    Craig

    (remove SPAM before emailing).


    "Craig L" <> wrote in message
    news:...
    > I use ASP to obtain data from a database and I have a piece of javascript
    > code I use on my website. I want to use the database variables in my
    > javascript. I'm not very familiar with javascript, but here is some info
    > similar to what I'm trying to do.
    >
    > <%
    > strFirstName=session("FirstName") ASP code
    > strLastName=session("LastName")
    > %>
    >
    > The javascript code I want to use the variables in.
    > Items[1]=["First Name", strFirstName,""]
    > Items[2]=["Last Name", strLastName,""]
    >
    > Please help or point me to a website with this solution. Thanks much for
    > your help.
    >
    > Craig
    >
    > (remove SPAM before sending)
    >
    >
     
    Craig L, Nov 10, 2003
    #12
  13. Jeff North wrote:

    > You can not use client-side javascript to read data directly from
    > a database.


    Depends. There is no native support but an additional API using HTTP
    requests could provide access to database on the server and another
    one could provide that for a database on the client.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 24, 2003
    #13
  14. Craig L

    Ivan Marsh Guest

    On Mon, 24 Nov 2003 23:04:27 +0100, Thomas 'PointedEars' Lahn wrote:

    > Jeff North wrote:
    >
    >> You can not use client-side javascript to read data directly from a
    >> database.

    >
    > Depends. There is no native support but an additional API using HTTP
    > requests could provide access to database on the server and another one
    > could provide that for a database on the client.


    Uh, you still wouldn't be reading the database with JavaScript then would
    you?

    --
    i.m.
    The USA Patriot Act is the most unpatriotic act in American history.
     
    Ivan Marsh, Nov 24, 2003
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sagejoshua
    Replies:
    16
    Views:
    676
    Michael Winter
    Nov 9, 2005
  2. Adel
    Replies:
    3
    Views:
    329
    Jack Klein
    Mar 17, 2005
  3. Replies:
    9
    Views:
    1,013
  4. Ravi Parimi
    Replies:
    16
    Views:
    218
    Thomas Kratz
    May 13, 2004
  5. Frank

    Using variables as variables

    Frank, Apr 9, 2007, in forum: Javascript
    Replies:
    4
    Views:
    137
    Dr J R Stockton
    Apr 10, 2007
Loading...

Share This Page