Home

ASP ADVANCED

In this chapter, you learn how to work with multiple Active Server Pages.  In the first section, you learn how to retrieve information entered into HTML forms.  In the second section, some methods for retrieving query strings are discussed.  In the third section, you learn how to redirect a user to a new page.  Finally, in the fourth section, you learn how to include one Active Server Page in another.

Retrieving the Contents of an HTML Form

You should think of HTML forms as the primary interface to your Active Server Pages applications.  The only reliable way to gather information from the visitors to your Web site is through an HTML form.

Suppose you want visitors to your Web site to register before they can use it.  To gather registration information, you should use a HTML form.  Here's an example:

<HTML>
<HEAD><TITLE> Register </TITLE></HEAD>
<BODY>
<H4>Registration:</H4>
<FORM METHOD="POST" ACTION="/regresults.asp">
<P>Please Enter Your First Name:
<BR><INPUT NAME="FirstName" TYPE="TEXT">
<P>Please Enter Your Last Name:
<BR><INPUT NAME="LastName" TYPE="TEXT">
<INPUT TYPE="SUBMIT"  VALUE="Continue">
</FORM>
</BODY>
</HTML>

This form simply asks for the first and last name of the visitor.  Once the information is entered, the Continue button can be clicked to move to the next page (see Figure below).

A Simple Registration Form

A Simple Registration Form

Once a user has entered information into an HTML form, you need a method of retrieving that information.  When an HTML form is submitted, it's submitted as part of a HTTP request. In the preceding example, the form would be submitted by using the request method POST.  The form is posted to the server.

Because an HTML form is submitted as part of an HTTP request, you can retrieve the contents of the form by using the Active Server Pages Request object.  The Request object has a particular collection for this purpose.  The Form collection contains all the information entered into an HTML form.

Each key in the Form collection corresponds to an input element in an HTML form.  For example, if a user completed and submitted the form in the preceding example, the Form collection would contain two keys.  The value of the first key would be the user's first name and the value of the second key would be the user's last name.  Consider the following script:

<HTML>
<HEAD><TITLE> Registration Results </TITLE></HEAD>
<BODY>
Thank you <%=Request.Form("FirstName")%> for registering!
</BODY>
</HTML>

When this page is displayed in a Web browser, the first name the user entered into the HTML form is displayed.  For this example to work, the ACTION attribute of the HTML form in the previous example must point to this page.

It's important to understand that you can't access the contents of an HTML form within the same page where the HTML form appears.  The HTML form must first be submitted before you can retrieve the values of the elements of the form.  In some situations, this is a significant limitation.  Whenever you have a page that has an HTML form, you must have an additional page that processes the contents of the form.

This extra page requirement explains why there are so many polite Web sites on the Internet. Typically, when you register at a Web site, an extra page at the end of the registration process thanks you for registering.  This extra page is actually required.  The final page is needed to process the information entered into the HTML form on the previous page.

Dumping the Contents of the Form Collection

A number of ways exist to retrieve all the contents of the Form collection.  If you want to loop through the contents of the collection and display each value, you could use the following script:

<%
FOR EACH name IN Request.Form
Response.write("<BR>"&name&"=")
Response.write(Request.Form(name))
NEXT
%>

This script displays each key-and-value pair in the Form collection.  If Bill Gates were to visit your Web site and use the registration form discussed in the previous example, the output of this script would look like this:

LASTNAME=Gates
FIRSTNAME=Bill

Notice how each of the key names has been converted to uppercase.  When you refer to an item in the Form collection, you don't have to worry about using the proper case.  For example, both FirstName and FiRsTnAme will retrieve the proper value.

Instead of using a FOR ... EACH loop, you can use a FOR ... NEXT loop to dump the Form collection.  The following script outputs the value of each key (but not the key name):

<%
FOR i=1 TO Request.Form.Count
Response.Write("<BR>"&Request.Form(i))
NEXT
%>

In this script, the count property of the Form collection is used to determine the number of items in the collection.  You can also use the count property to discover exactly how many of the HTML form fields were completed when the form was submitted.

Finally, if you simply want to return the items in the Form collection in a single URL-encoded string, you can use the following script:

<%=Request.Form%>

This single-line script returns the form data in unparsed form.  Following is an example of the output from the script:

FirstName=Bill&LastName=Gates

Notice that the case of the form field names is preserved.  This method of returning the items in the Form collection returns the body of the HTTP POST request in unparsed form.

Form Elements with Multiple Values

Certain HTML form elements can have more than one value.  For example, both check boxes and list boxes can have multiple values.  Consider the following HTML form:

<FORM METHOD="POST" ACTION="/regresults.asp">
How
did you hear about our web site?
<BR><INPUT NAME="HowHear" TYPE="CheckBox" VALUE="A Newspaper Article">
A Newspaper Article
<BR><INPUT NAME="HowHear" TYPE="CheckBox" VALUE="A Search Engine">
A Search Engine
<BR><INPUT NAME="HowHear" TYPE="CheckBox" VALUE="A Friend">
A Friend
<BR><INPUT NAME="HowHear" TYPE="CheckBox" VALUE="Stumbled
Into It">
Stumbled Into It
<P><INPUT TYPE="SUBMIT" VALUE="Continue">
</FORM>

This form can be used to determine how visitors to your Web site discovered it.  But someone may have found out about your Web site in more than one way.  For example, someone may have heard about your Web site both from a friend and from a newspaper article.  This form allows the user to select more than one value at a time (see Figure below).

A form with multiple values

A form with multiple values

How can you retrieve the value of a form element when it can have more than one value?  You can use an additional parameter of the Form collection.  Look at this example:

<HTML>
<HEAD><TITLE> Your Response </TITLE></HEAD>
<BODY>
According to your response, you heard about this web site in
<%=Request.Form("HowHear").Count%> ways.
<P>You heard about this site from:
<%
FOR EACH way IN Request.Form('HowHear")
Response.Write("<P>"&way)
NEXT
%>
</BODY>
</HTML>

In this script, the count property is used to return the number of check boxes selected.  In this example, the Count property doesn't return the total number of items in the Form collection.  Instead, it only returns the total number of values of the form element named HowHear.

The FOR ... EACH loop iterates through each value.  For example, if someone selects both A Friend and A Newspaper Article, both of these values are displayed.

Text Areas and the Form Collection

You can retrieve the text entered into a text area in the same way as any other form element.  Remember that VBScript string variables can be quite long.  There is no 255-character limitation on the string length, as in many other computer languages.

This is an example of an HTML form with a text area:

<FORM METHOD="Post" ACTION="/Response.asp">
Please enter any feedback on this web site below:
<P>
<TEXTAREA NAME="feedback" COLS=30 ROWS=10></TEXTAREA>
<P><INPUT TYPE="SUBMIT" VALUE="Submit Feedback">
</FORM>

This HTML form displays a text area that can be used for user feedback on a Web site.  When the user clicks the button labeled Submit Feedback, he or she is brought to the Response.asp page.  If you want to display the text entered into the feedback text area in the Response.asp page, you can do it like this:

<HTML>
<HEAD><TITLE> Feedback Response </TITLE></HEAD>
<BODY>
Thank you for submitting feedback.  You wrote:
<P>
<%=Request.Form("Feedback")%>
</BODY>
</HTML>

HTML Tags and Forms

A user can enter any text into either a single-line HTML text field or a multi-line HTML text area.  In particular, nothing prevents a user from entering HTML tags.  This can be both good and bad.

In some situations, you want a user to be able to format the text entered into a form by using HTML tags.  For example, imagine that you have created a bulletin board for your Web site using Active Server Pages and HTML forms.  The postings on the bulletin board may be more interesting if the text is displayed using different colors and different fonts.  To format the text in a posting, someone posting to the bulletin board can simply use HTML tags, You don't have to do anything special to allow this to happen.

In other situations, you may not want HTML formatting to be applied.  For example, suppose you're building a Web site that explains how to program using Active Server Pages. (You might call this Web site the ASP site and locate it at http://www. aspsite.com.) If someone posts a code example using HTML, you want the actual HTML tags to be displayed and not interpreted.  How can you do this?

Fortunately, Active Server Pages includes a special method for this particular purpose.  The Server.HTMLEncode( ) method translates HTML tags into HTML character codes.  Check out this example of how this method is used:

<%=Server.HTMLEncode("<B>This is bold</B>")%>

Normally, if the string "<B>This is bold</B>" is outputted to a browser, the text would appear as bold.  However, once the text has been HTML-encoded, the actual string is outputted instead.

Testing for the Existence of Form Elements

You'll often need to check whether a person has actually completed all the information in a form.  For example, a normal registration form has a number of required fields.  You don't want to allow a visitor to your Web site to gain access by submitting a blank registration form.

You can check whether information has been entered into a particular form field by using a script like the following:

<%
IF Request.Form("FirstName")=" " THEN
Response.Write("You must enter your first name.")
ELSE
Response.Write("Thank you for registering")
END IF
%>

This script tests whether the form field named FirstName has a value.  The item FirstName is compared to a zero-length string.  If the user has neglected to enter a first name, he or she is informed of this fact.

When someone enters incomplete form information, you should redirect the user to the form once again, so the information can be entered.  You could do this by supplying a hypertext link back to the proper page.  However, it would be much better for the user to be returned to the proper page automatically.  You'll learn some methods of doing this later in this chapter. (See "Redirecting a User to Another Page" and "Including Files.")