Session Variables

Cinnamon
06-28-2002, 02:09 PM
I have 3 asp pages...

1) prompts the user for first and last name

2) prompts the user for a greeting

3) writes the results of the imput to the screen

But it doesn't work. My goal is to able to set a variable once and then use that data on other pages within the same session.

new_1.asp

<%
Session("FName")=(request.form("firstname"))
Session("LName")=(request.form("lastname"))
%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New 1</title>
</head>

<body>
<form action="new_2.asp" method="POST">
First Name: <input type ="text" name="FirstName">
Last Name: <input type ="text" name="LastName">
<input type ="submit" value="send">
</form>
</body>

</html>


new_2.asp

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New 2</title>
</head>
<body>

<form action ="new_3.asp" method="post">
Salutation: <input type="text" name="greeting">
<input type="submit" value="send">
</form>

</body>
</html>


new_3.asp

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New 3</title>
</head>

<body>
<%
response.write(request.form("greeting"))
response.write("&nbsp")
response.write(FName)
response.write("&nbsp")
response.write(LName)
%>
</body>

</html>



Or, if anyone can show me another way of accomplishing the same thing I'd really appreciate it.

Cinnamon

Rezner
06-28-2002, 03:42 PM
<%
Session("FName")=(request.form("firstname"))
Session("LName")=(request.form("lastname"))
%>This needs to be on your second page instead of your first.

Cinnamon
07-01-2002, 08:32 AM
Hmmm... I tried that and it still didn't work.

Robby
07-01-2002, 10:16 AM
Session vars must be in a file named global.asa

you can read more here http://www.w3schools.com/asp/asp_globalasa.asp

Jaws
07-01-2002, 11:03 AM
As Reznor said, this has to be on the second page:
<%
Session("FName")=(request.form("firstname"))
Session("LName")=(request.form("lastname"))
%>


And the other problem is in new_3.asp, you need change the response.write statements to use the session variables like below:


<%
response.write(request.form("greeting"))
response.write(" ")
response.write Session("FName")
response.write(" ")
response.write Session("LName")
%>

Cinnamon
07-01-2002, 01:38 PM
This has been really helpful. I'm learning alot. But I'm ready to ask the next dumb question. I have added a Session for RecordID which gets input on my 1st page (RecordNum). I can't seem to get the RecordID to be recognized as a number. I know there is probably a really easy solution to this.

new_1.asp

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New 1</title>
</head>

<body>
<form action="new_2.asp" method="post">
First Name: <input type ="text" name="FirstName">
Last Name: <input type ="text" name="LastName">
Enter Record Number: <input type ="text" name="RecordNum">
<input type ="submit" value="submit">
</form>
</body>

</html>


new_2.asp

<%
Session("FName")=(request.form("FirstName"))
Session("LName")=(request.form("LastName"))
Session("RecordId")=CLng(request.form("RecordNum"))
%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New 2</title>
</head>
<body>

<form action ="new_3.asp" method="post">
Salutation: <input type="text" name="greeting">
<input type="submit" value="submit">
</form>

</body>
</html>


new_3.asp

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New 3</title>
</head>

<body>
<%
If IsNumeric("RecordId") then
response.write(request.form("greeting"))
response.write("&nbsp")
response.write Session("FName")
response.write("&nbsp")
response.write Session("LName")
response.write("&nbsp")
response.write Session("RecordId")
Else
response.write("This is text not a number!")
End If
%>
</body>

</html>


When I ask RecordId is numeric I keep getting "This is text not a number" error.

Cinnamon

Thinker
07-01-2002, 02:08 PM
You aren't loading RecordId anywhere on new_3.asp before trying
to use it.

Cinnamon
07-01-2002, 02:23 PM
I'm not sure I understand "loading RecordId". But I did add the line...

Response.Write Session("RecordID")

before the If statement.

The result is:
I entered the number 1 as a record number and this was the result.
"1 This is text not a number"

Therefore it is writing the number one but not recognizing it as a number.

Thinker
07-01-2002, 02:35 PM
Something like...

Dim RecordId
...
RecordId = Session("RecordId")
...

You have to keep remembering that no data is automatically
moved/restored between page loads. When new_3.asp is called
from the browser, it only knows what is in the ASP objects. All
variables are empty.

Jaws
07-01-2002, 02:59 PM
I think you are getting a little confused with your use of session variables. You can't reference a session variable without explicitly declaring the session() wrapper.

ie..
If IsNumeric("RecordId") then

this should be written

If IsNumeric(session("RecordId') ) then

In this example RecordId is null but session("RecordId") is your value. Basically, RecordId is being interpreted as a different and seperate variable than session("RecordId"). When you did your response write you added the session("RecordId") which was correct.

This is a pretty common mistake when you are starting out.

Cinnamon
07-01-2002, 03:26 PM
Thanks... that was a big help. Now I'm off and running again. I'm using the concept on a much bigger project. But I really needed to get the basics straight. Thanks again!

Cinnamon

Rezner
07-01-2002, 03:27 PM
Originally posted by Robby
Session vars must be in a file named global.asa

you can read more here http://www.w3schools.com/asp/asp_globalasa.asp to clarify: session vars don't have to be in the global.asa. They can be given value anywhere in your site structure. The global.asa is involved with the session only in it's start/end operations... and that is it.

As for your error, Cinnamon, you have the right syntax but you are committing a conceptual error. You are attempting to set the Session variable values with input objects that are located on the same page. The Session variables can not be set without first submitting the form.

You could, as is common practice, ACTION the form back onto the same page. That way, if any errors occur -- you can display them by the input objects. Or you could do so with a different page. But, regardless of which... the ACTION only directs the info to a specific page. The Request object is then used to extract the information from the submitting page. So, in a nutshell... you can't use the Request object unless the page is first submitted.

Cinnamon
07-01-2002, 03:33 PM
I have been to the site you recommended and am reading about global.asa. I'm going to tear this code apart and get it right. I really appreciate all the input.

Thanks again,

Cinnamon

Cinnamon
07-02-2002, 11:25 AM
I wanted take a moment and thank everyone for all of the help. I was able to take the information from this post and many others (most of you I'm sure are tired of hearing from me) and create an SQL database that it accessible over the web for our staff to update. The database incorporates 1/2 million images from our library records. Which will be accessible to patrons in the next few months. Everything is working great. I have a few cosmetic changes to make but the "higher ups" are really impressed. I've learned alot and had fun too! I couldn't have done it without you!

Thanks to everyone!

Cinnamon

Robby
07-02-2002, 03:38 PM
Originally posted by Cinnamon
...most of you I'm sure are tired of hearing from me...

Not at all, this what we/I enjoy doing.

I even learned something new today about 'not needing a global.asa for a
session variable'. (It's just the way I was used to using them)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum