ASP Forms

In our last tutorial we learned some of the basics of ASP, like syntax, printing data to the screen, how to create and manipulate variables, and even how to make arrays. I even slipped a for loop in on you to further boggle your puny brain. In this article, I detail the use of forms in ASP and how to retrieve user input.


If you didn’t cheat and answered the questions in my first tutorial, then you should have a grasp of how to create forms in HTML. Here, I am going to teach you to retrieve the data the user enters into those forms. The first method we will discuss is the request.querystring.


The Request.QueryString command is used to retrieve values in forms with the Get Method (method=”get”). That’s similar to the method my girlfriend uses every time I get near near her, except that method is the Git! Method and involves a lot of door slamming.

Data received from a form that utilizes the Get Method is visible to the public, as it is contained in the URL, and can only store limited information. Here is an example of how to use the Request.QueryString command:

<form action="sample.asp" method="get">
Please Enter Your Name: <input type="text" name="name"/>
<input type="submit" value="Submit" />
dim name
If name<>"" Then
Response.Write("Hello " & name & "!<br />")
Response.Write("You are my lord and savior!")
End If


The above code asks the user for their name and stores the value when the user clicks the Submit button. It then takes that value and says if the value is not equal to (<>)””(or basically so long as the value is not null), then write “Hello” to the screen and append the text in the variable “name,” followed by more text: “You are my lord and savior”. If the user does not enter any text into the text field, nothing will happen.

Here is another example. First, let’s create our HTML form:

<form method=”Get” action=”sample.asp”

Enter Your Name: <input type =”text” name =”name”/>

Enter Your Occupation: <input type =”text” name =”occupation”/>

<input type=”Submit” value=”Click to Submit”/>


Next, we will create our sample.asp page to process the information:



Dim name, occupation



response.write(“Your name is: ” & name & “<br />”)

response.write(“Your current occupation is ” & occupation & “<br />”)


If the user entered their name as Freddy Krueger and their Occupation as Crazy-assed Murderer, the following would be output to the screen:

  Your name is: Freddy Krueger

  Your current occupation is Crazy-assed Murderer


To collect data from forms that used the Post method (method=post), we use the Request.Form command. Unlike the Request.QueryString, the Request.Form command is invisible to the user (and thus will not appear in the URL), and there are no limits to the amount of data you can return.

To begin, let’s create our HTML page with the form on it:

<form method=”Post” action=”anothersample.asp”>

Enter Your Name: <input type=”text” name=”name”/>

Enter Your Occupation: <input type=”text” name=”occupation”/>

<input type=”submit” value=”Click to Submit”/>


Next we create our ASP page (anothersample.asp) to retrieve the data:



Dim name, occupation



response.write(“Your name is: ” & name & “<br />”)

response.write(“Your present occupation is: ” & occupation & “<br />”)


This will result in the following printing out to your browser (if the user had supplied the name Jason Voorhees and the occupation Camp Counselor):

  Your name is: Jason Voorhees

  Your present occupation is: Camp Counselor

{mospagebreak title=Cookies}

Cookies are truly the breakfast of champions. If you’ve ever watched an episode of Pig Olympics, you know what I am talking about. And if you haven’t, head over to YouTube and give it a search. And while you are at it, check out a weightlifter by the name of Ronnie Coleman. Guaranteed in no time you will be shouting YEAP and LIGHTWEIGHT at the top of your lungs.

Aside from being delicious and highly nutritious, Cookies are small files that the villainous server places on the user’s computer. Take that user! Whenever the user logs back onto the web page, it sends that cookie back (who wants a used cookie?). This allows us to “remember” certain aspects about the user.

Here is how you create a cookie in ASP:




This stores the name “Monster” in the name cookie. We can also determine how long the cookie will stay on the user’s computer, by assigning an expiration property to it:



Response.Cookies(“name”).Expires=#April 22, 2008#


This code creates the cookie and stores the value in it, then gives the cookie an expiration date.

Getting a Cookie from the Cookie Jar

It’s one thing to create a cookie, but another to retrieve that cookie without getting caught by our parents at four in the morning. I mean come on: I’m thirty. Can’t I have a cookie when I want one?

We use the Request.Cookies command to jack those cookies. Here is how we retrieve a cookie and then display it for all the world to see:



response.write(“Leave my cookies alone you ” & username)


This would retrieve the cookie from the users computer and display the following to the screen:

  Leave my cookies alone you Monster

{mospagebreak title=A Baker’s Dozen}

Bakers must be dumb. I mean don’t get me wrong: I love the incredible and all important work they do. But don’t they know that a dozen is only equal to twelve and not thirteen? But who cares, we get that extra donut free of charge. So stick it to the baker. And shh…don’t let them know.

If you want to store more than one piece of data in a cookie, you can. I won’t stop you. When you do so, it is called a collection and the values inside are referred to as keys.

Here is how we create a cookie named “fortune” with multiple values:


Response.Cookies(“fortune”) (“fortuneone”) = “You will die a horrible death. Have a good day”

Response.Cookies(“fortune”) (“fortunetwo”) = “You will meet a handsome stranger. Then he will rob you”

Response.Cookies(“fortune”) (“fortunethree”) = “Life is like a box of vacuum cleaners. It sucks.”


Reading the Collection

Here, we learn to read the keys in our Cookie collection:




Dim x, y

for each x in Request.Cookies


If Request.Cookies(x).HasKeys then

for each y in Request.Cookies(x)

response.write(x & “/” & y & “=” & Request.Cookies(x) (y))

response.write(“<br />”)



Response.Write(x & “=” & Request.Cookies(x) & “<br />”

end if

response.write “</p>”





How is that for some wacky code. Here is the result:

  fortune/fortuneone=You will die a horrible death. Have a good day

  fortune/fortunetwo=You will meet a handsome stranger. Then he will rob you

  fortune/fortunethree=Life is like a box of vacuum cleaners. It sucks.

{mospagebreak title=Sessions}

A good way to visualize a session is to think of opening a program. That would start your session. Then you work in it and close it. That would be the end of your session. A Session Object stores data about a user session, and can change settings for that user session. When you begin a session in ASP, it creates a cookie with information about the user. The information is available to all pages within the application. Types of data stored are typically the user’s name and any preferences they may have. The session object is destroyed after the session is over.

Beginnings and Endings

As I said, sessions have a beginning and an ending. Look at these two nifty bulleted lists that outline their time line:

A session begins:

  • If the user requests an ASP file with a Global.asa file that contains a Session_OnStart procedure.
  • Whenever a value is stored in a Session variable.

  • When a user requests an ASP file with a Global.asa that contains the object tag that will “instantiate” a particular object with sessional scope.

A session ends:

  • When the user has made no requests or has not refreshed a page within the application for a period that you specify. Note that this value is 20 minutes by default.

Setting the Session Timeout

You can change the amount of time before your session times out by using the Timeout property. Here is how that works:




{mospagebreak title=How to Store Sessions and Retrieve Them}

You store variables in your session object. For instance, let’s say we wanted to store the user’s name in an object. Here is how we would do that:


Session(“name”)=”Michael Bolton!”



Now that the variables are stored in the session object, we can use them for any page within our application. If we wanted to display a greeting on our home page, we could do so by inserting the following code on the page:

Greetings <%Response.Write(Session(“name”))%>

Your music is <%Response.Write(Session(“music”))%>

This would result in the following being displayed in the user’s browser:

  Greetings Michael Bolton!

  Your music is Horrible

Well, we covered a lot of ground in this article, and there is still a lot more to discuss with sessions and cookies, but that will have to wait for next time. In our next article, we will continue our discussion of sessions, and cover applications as well.

Till then…

2 thoughts on “ASP Forms

[gp-comments width="770" linklove="off" ]