Making Smart Pages

Submitted on: 1/5/2015 6:48:00 AM
By: Glenn Cook (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: ASP (Active Server Pages)
Views: 524
     Sending and retrieving data between web pages is the key to developing functional web-applications. This tutorial will show you how to utilize the session in your "weblications" (that's my new favorite geek-slang) using a mix of some basic ASP and the HTTP protocol. I will show you how to get info from a user in "page1.asp" and extract it again later in "page5.asp." I will address some of the techniques you see used at popular websites like, many popular search engines, and just about any site that is interactive. I am assuming that you have some experience with HTML.


Sending Data

First, let's try to understand how we send the data from page to page. The data is sent using two possible methods with HTTP (Hypertext Transfer Protocol). You can pass data with HTML tags using the FORM "POST or GET" methods, or with hyperlinks.

  1. If you use the FORM method you basically tell the browser to wrap up a bunch of input box information within the form tags on your web page and post everything to a script that can accept and translate the information sent.
  2. If you are sending data using a hyperlink, you have to customize the hyperlink to declare and define a string variable. It's easy! All you do is point your hyperlink to the URL, you add a question mark, and you add your variable. Test this example:
  3. For another example go to and look at the HTML source for that page and find the form post code. See how it names the input fields? Now, go back to the actual page and submit a ticker symbol like AMZN ( my pick of the month) and click "Get Flash Quotes." If you look at the Url in your browser, you'll notice a bunch of variables that you sent to the page. If you only put in one ticker symbol, that will be the only one you see listed- the rest of the variables will be empty.

Requesting the Data

With ASP, all you have to do to get your data is to "Request" it. When you set up the asp script page to receive the data, you usually want to use a "If Then" or a "Select Case" statement at the top of your page. But before I get into the semantics of these statements let's just get the data!

If a form is sending the data you use this asp code to grab each input field's data:

  • <% Request.Form("NameOfField1")%>

Or you can print the field's data to a page using this code anywhere in your asp script page:

  • <%=Request.Form("NameOfField1")%> (Notice that little "=" sign? Yep, that's all you need to know to print asp data into your HTML code.)

If the data is being sent by a hyperlink use this code to grab the variable:

  • <% Request.Querystring("NameOfVariable") %>

Likewise, you can print the data anywhere in the page using:

  • <%=Request.QueryString("NameOfVariable")%>

Ok, so you know how to get the data, now what do you do with it?

Well, the sky is the limit! Manipulating this data is how you make smart webpages. You can use the data to write cookies, connect to databases and extract certain recordsets, or you can control the HTML code that prints to the user's screen, etc. In this tutorial though I will focus on extracting the user's information early in the visit to the site, and how you can extract that data anytime during the session-not unlike a shopping cart tracks the contents of your order.

ASP lets you create some global variables at the beginning of the user session in the Global.asa file. When you hit an ASP site the first thing ASP does is look to the Global.asa file to get any important information it needs to know for that user session. A session begins when a user first visits the site after the OnStart event fires in the Global.asa file. The Global.asa file is like the config.sys file in DOS when you first turn on your computer. It configures the user session, and lets you free up memory space for any variables you might want. Each user session lasts as long as the person is visiting the site. The session ends when the user closes the browser, after an amount of time set by the system administrator, or by code telling ASP to end the session. What you want to do with the Global.asa is free up some memory space by declaring a few (or many) variables. As long as the session exists, the memory space for these variables will also exist. Logically, anytime you want to know what information is contained within these session variables you can extract it. Just as easily you can write information to these variables anytime. (See where I'm going with this?)

In your Global.asa file you&rsquo;ll find the Session_OnStart event and this is where you want to create your variables.

Sub Application_OnStart
End Sub
Sub Application_OnEnd
End Sub
Simplified: This code executes the very first time a user hits the weblication and ends some time after the last person leaves.
Sub Session_OnStart
Session("Variable1") = 0
Session("Variable2") = 0
Session("Variable3") = 0
End Sub
This code is what you're concerned with. When a user hits your site, ASP goes right to the Global.asa and fires this event to start a session. We've also declared a few variables that we will use while the user is at the site. These variables end when a user ends the session.
Sub Session_OnEnd
End Sub
This code fires at the end of a user session.

This is really all there is to it. Now you can use the information you extract from the user and write it to one of these variables. I would use this code at the beginning of the asp script that receives the information sent by a form. All I&rsquo;m doing is writing the submitted data to the session variables, which I can extract anytime during the session.

dim variable1
dim variable2
dim variable3

variable1 = Request.Form("Field1")
variable2 = Request.Form("Field2")
variable3 = Request.Form("Field3")

Session("Variable1") = variable1
Session("Variable2") = variable2
Session("Variable3") = variable3

Now you can print that variable to a page anytime by using the code, <%=Session("Variable1")%>. You can also extract that information to use in an SQL statement:
dim MartinPrince
MartinPrince = Session("Variable1")
<% "SELECT * FROM Employees Where FirstName = '" & MartinPrince & "'" %>

Other 7 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.