Using ASP Variables

C

Craig L

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

(e-mail address removed) (remove SPAM before sending)
 
B

bengee

Craig said:
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
 
C

Craig L

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 said:
Craig said:
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
 
B

bengee

Craig said:
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
 
C

Craig L

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
 
B

bengee

Craig said:
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.
 
J

Jeff North

| 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
 
G

Grant Wagner

Craig said:
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

(e-mail address removed) (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 <[email protected]>

* 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
 
J

Jeff North

| 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.
| | > On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
| >
| > >| 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
| >
| >
| > ---------------------------------------------------------------
| > (e-mail address removed) : Remove your pants to reply
| > ---------------------------------------------------------------
|
 
C

Craig L

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 said:
| 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.
| | > On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
| >
| > >| 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
| >
| >
| > ---------------------------------------------------------------
| > (e-mail address removed) : Remove your pants to reply
| > ---------------------------------------------------------------
|
 
C

Craig L

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

Craig said:
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

(e-mail address removed) (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 <[email protected]>

* 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
 
C

Craig L

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

Craig

(e-mail address removed) (remove SPAM before emailing).
 
T

Thomas 'PointedEars' Lahn

Jeff said:
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
 
I

Ivan Marsh

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top