|
 |
asp_databases thread: "Fun" with Sessions...NOT! >:(
Message #1 by "Susan Henesy" <susan_henesy@u...> on Mon, 22 Oct 2001 18:07:38
|
|
Hi everyone,
I'm working on a project that involves Session Variables. What I would
like to know is how I can change some Session Variables on one of my web
pages, using JavaScript to pass some values to the S.V.s.
One of my coworkers has hardcoded some values on her web page, and it
works fine. Her code looks like this:
<%
Session("UserID") = "Me"
Session("sessYr") = "2001"
Session("sessMo") = "08"
%>
What I would like to do is very similar, except the Session values would
be dependent on what button you clicked on a form. So my code, which does
NOT work, looks like this:
------------------------------------------------------
function fncSubmit(strReportName, intYear, intMonth){
// Load a Crystal Report, based on the
// name in strReportName; pass the Year and Month
// into the Session variables "sessYr" and "sessMo"
<%=Session("sessMo")%> = intMonth
<%=Session("sessYr")%> = intYear
LoadWindow("../reports/" + strReportName);
}
------------------------------------------------------
This doesn't work because the Session variables just give values, and the
code thinks I am trying to set a value equal to something, so what I
actually get is:
08 = 10
1999 = 2001
There's just got to be a simple answer to this, augh! I'll keep trying
different angles, but in the meantime, has anyone got any suggestions???
Many thanks,
Susan
Message #2 by Imar Spaanjaars <Imar@S...> on Mon, 22 Oct 2001 22:01:05 +0200
|
|
Hi Susan,
You can't influence Session Variables from client side code directly. The
client side runs on the client, and Session Variables run, well, on the server.
The only way to do what you want is to wrap your JavaScript values in some
form, submit it to the server and use either the QueryString or the Form
collection of the Request object (depending on whether you set your form's
action to GET or POST).
Here is a very simple sample (code real-time here with no checking) using
the POST method
Client page (myTest.html for example):
<script language="JavaScript" type="text/javascript">
function SubmitMe(sValue)
{
document.forms(0).txtHidden1.value = sValue;
document.forms(0).submit();
}
</script>
<form id="frmTest" name="frmTest" action=myActiverServerPage.asp"
method="post">
<input type="hidden" id="txtHidden1" name="txtHidden1" />
<input type="button" value="Button 1" id="btnSubmit1"
name="btnSubmit1" onclick="JavaScript:SubmitMe('Button 1'" />
<input type="button" value="Button 2" id="btnSubmit2"
name="btnSubmit2" onclick="JavaScript:SubmitMe('Button 2'" />
</form>
Now, when you click one of the two buttons, you fill a hidden form field
with the value you pass to the SubmitMe function. That value will be the
string "Button 1" for button 1, and, obviously, "Button 2" for button 2.
Then the page will be posted to myActiverServerPage.asp
There you can retrieve the value like this:
Server page (myActiverServerPage.asp):
Dim sValue
sValue = Request.Form("txtHidden1") & ""
If Len(sValue) > 0 then ' they submitted a value
Session("MySpecialValue") = sValue
end if
When you submitted the form by clicking Button1, sValue will now contain
the string "Button 1", which you can either use as a variable in the rest
of your page, or write out to the screen like this:
Response.Write(sValue)
or
<%=sValue%>
This is just one way to do stuff like this. There are many others as well,
but IMO this one is pretty simple and effective.
Hope this helps,
Imar
At 06:07 PM 10/22/2001 +0000, you wrote:
>Hi everyone,
>
>I'm working on a project that involves Session Variables. What I would
>like to know is how I can change some Session Variables on one of my web
>pages, using JavaScript to pass some values to the S.V.s.
>
>One of my coworkers has hardcoded some values on her web page, and it
>works fine. Her code looks like this:
>
><%
>Session("UserID") = "Me"
>Session("sessYr") = "2001"
>Session("sessMo") = "08"
>%>
>
>What I would like to do is very similar, except the Session values would
>be dependent on what button you clicked on a form. So my code, which does
>NOT work, looks like this:
>
>------------------------------------------------------
>function fncSubmit(strReportName, intYear, intMonth){
>// Load a Crystal Report, based on the
>// name in strReportName; pass the Year and Month
>// into the Session variables "sessYr" and "sessMo"
>
><%=Session("sessMo")%> = intMonth
><%=Session("sessYr")%> = intYear
>
>LoadWindow("../reports/" + strReportName);
>}
>------------------------------------------------------
>
>
>This doesn't work because the Session variables just give values, and the
>code thinks I am trying to set a value equal to something, so what I
>actually get is:
>
>08 = 10
>1999 = 2001
>
>There's just got to be a simple answer to this, augh! I'll keep trying
>different angles, but in the meantime, has anyone got any suggestions???
>
>Many thanks,
>Susan
>
Message #3 by Steve Carter <Steve.Carter@t...> on Tue, 23 Oct 2001 10:52:59 +0100
|
|
The tricky part of what you're doing is that ASP gets evaluated by the
server, but javascript is evaluated on the client, i.e. the browser.
[Note to pedants: I know this is not strictly true, but in practice this is
what happens and is close enough to explain this problem.]
Imagine the ASP is a web page author which writes the page that is delivered
to the client. This means that the javascript is treated in the same way as
the HTML by ASP, it is simply passed to the browser as is.
So, if you have
<%
dim a
a = "<P onclick=""alert('clicked')"">A Paragraph</P>"
%>
<TABLE><TR><TD><%=a%></TD></TR></TABLE>
which evaluates to
<TABLE><TR><TD><P onclick="alert('clicked')">A
Paragraph</P></TD></TR></TABLE>
then this last string is all the client side ever sees.
Similarly, where you have
<%=Session("sessMo")%> = intMonth
Say Session("sessMo") is 10 then the client only gets
10 = intMonth
which is clearly no good.
The only way to get values back to the server is by submitting a form. So
you would pass out your values like this
<FORM>
<INPUT TYPE="HIDDEN" NAME="inpSessMo" VALUE="<%=Session("sessMo")%>">
</FORM>
then your Js would be able to go
inpSessMo.value = intMonth
note that this is setting the value of the hidden form element, still in the
browser. When you submit the form, then your ASP on the target page can
talk to the session contents:
<%
Session("sessMo") = Request.Form("inpSessMo")
%>
Of course this is slightly simplified for clarity and you will probably need
additional checks on the presence and validity of the data but I hope it
clears up the problem re: client- and server-side scripting.
Steve
> -----Original Message-----
> From: Susan Henesy [mailto:susan_henesy@u...]
> Sent: 22 October 2001 19:08
> To: ASP Databases
> Subject: [asp_databases] "Fun" with Sessions...NOT! >:(
>
>
> Hi everyone,
>
> I'm working on a project that involves Session Variables.
> What I would
> like to know is how I can change some Session Variables on
> one of my web
> pages, using JavaScript to pass some values to the S.V.s.
>
> One of my coworkers has hardcoded some values on her web page, and it
> works fine. Her code looks like this:
>
> <%
> Session("UserID") = "Me"
> Session("sessYr") = "2001"
> Session("sessMo") = "08"
> %>
>
> What I would like to do is very similar, except the Session
> values would
> be dependent on what button you clicked on a form. So my
> code, which does
> NOT work, looks like this:
>
> ------------------------------------------------------
> function fncSubmit(strReportName, intYear, intMonth){
> // Load a Crystal Report, based on the
> // name in strReportName; pass the Year and Month
> // into the Session variables "sessYr" and "sessMo"
>
> <%=Session("sessMo")%> = intMonth
> <%=Session("sessYr")%> = intYear
>
> LoadWindow("../reports/" + strReportName);
> }
> ------------------------------------------------------
>
>
> This doesn't work because the Session variables just give
> values, and the
> code thinks I am trying to set a value equal to something, so what I
> actually get is:
>
> 08 = 10
> 1999 = 2001
>
> There's just got to be a simple answer to this, augh! I'll
> keep trying
> different angles, but in the meantime, has anyone got any
> suggestions???
>
> Many thanks,
> Susan
Message #4 by "Susan Henesy" <susan_henesy@u...> on Tue, 23 Oct 2001 15:09:07
|
|
Thank you for your help! I felt sort of sheepish about that question
after I posted it. In fact, I recognized shortly afterward that "POST"-
ing was my answer! *Sigh*....I often wish this site had an option to
delete one's silly questions ;).
I'm more limited on this project, since I am working with a team, and they
want everything done a certain way, so I thought I wasn't going to be
allowed to post a Session value to the next page. But all is well, I got
the green light for posting once I showed a team mate the issue of trying
to interact between the client and the server, and so I can now retract my
silly question. BUT you both gave me some very good feedback. AND your
posts back up what I thought I needed to do, so my conviction about
posting is reinforced by you experts, and thank goodness for that!
This is my first time working with Sessions, so they leave me a little
confused, I must say! I am sure everything will work out fine. NOW if we
could only find out why Crystal Reports is driving us all mad, we probably
would solve the last of our big problems.
Thanks again for your help, I very much appreciate your feedback!
Susan
|
|
 |