the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

home / articles / asp

Using the Request Object

The Request object can be easily referenced in your ASP script. As a matter of fact, whenever writing an ASP script, you can assume that this object and its properties are always available to you.   In this article we'll use VBSCRIPT as the base language for interacting with this object.

Our first example uses two properties of the Request object: the Form and QueryString collections:

Form

The Form property is actually a collection of name/value pairs.   When I say collection, I'm referring to a group of related objects stored in an associative array.  In this case, referring to a member of the Form collection by its associated name, gives you access to an element in this array and  its associated data value.

QueryString

The QueryString property is a collection as well, based on the same associative array technology as the Form collection.  However, there are some differences.

Forms

Both the Form and QueryString properties are specifically related to the processing of HTML forms and the data fields contained therein.  Here's the difference, if you submit data from an HTML form using  the POST method, the Form collection is populated with the name value pairs from the form.  On the other hand,  if you submit data from the form using the GET method,  the QueryString collection is populated instead.

For starters, here's an example HTML form:


<FORM METHOD="POST" ACTION="MYREQUEST.ASP">
    <P>Field1: <INPUT TYPE="TEXT" NAME="Field1" SIZE="20">
    <P>Field2: <INPUT TYPE="TEXT" NAME="Field2" SIZE="20">
    <P><INPUT TYPE="SUBMIT" VALUE="SUBMIT" NAME="MYSUBMIT">
</FORM>


For those of you  new to HTML forms, I'll take a second to explain.   First, this form contains two input fields and one button.  The form begins with a <FORM> tag which accepts two parameters, a METHOD, and an ACTION.  The METHOD tells the browser how to 'package' the data in the form when sending the HTTP request message to the server, the ACTION identifies the server process that will accept, or process the posted data.

The next several lines consist of several variations on the <INPUT> tag.   Notice that each <INPUT> tag accepts parameters that determine the characteristics of the input widget or control.  The TYPE of the INPUT determines the style of input available such as free-form text or button and the NAME parameter determines how the browser will package and associate this INPUT with its associated data.

The SUBMIT type of INPUT defines a special-purpose button, that when pressed will instruct the browser to package the data and send it to the server in the form of name/value pairs.  The form is completed with the ending </FORM> tag.

MYREQUEST.ASP

The ACTION attribute of the FORM tag specifies the server process that will be requested when the browser submits this form. 

<FORM METHOD="POST" ACTION="MYREQUEST.ASP">

From our perspective, this attribute is important because it contains the name of the ASP script that will do the processing and will receive HTTP request data.

Here's the contents of MYREQUEST.ASP:


<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<TITLE>Processing Forms With ASP!</TITLE>
</HEAD>
<BODY>
<P>Field1 = <% =Request.Form("Field1") %>
<P>Field2 = <% =Request.Form("Field2") %>
</BODY>
</HTML>


The two most important lines of this script begin immediately after the <BODY> tag.  Inside the beginning and ending scripting tags are references to two of  the Form collection's variables: Field1 and Field2.

As you might have guessed, this is the syntax for retrieving values from a member of this collection.  Using this syntax you can retrieve values from any number of field values passed to your ASP script.  Notice that the Form collection is indexed or subscripted by the name of the field.  Remember this notation, because accessing any collection of the Request object will be accomplished  in the same fashion.

somevariable = Request.Form("Field1")

Consider another abbreviated example:


<%
Dim x
x =Request.Form("HoursWorked") * Request.Form("HourlyRate")
%>

Your wages are:  <% =x  %>


Notice that you can be very creative with the usage of field values from the Form collectionFor those of you already well-versed in VB, notice the cardinal sin just committed when I dimensioned the variable 'x'.  I declared a variable without its respective type specifier.

Well, before I'm hung out to dry, it turns out, that in VBSCRIPT all variables are declared this way, because all variables are VARIANT.  This might take some getting used to if you're a stickler for type safe programming.  We'll talk more about these and other syntax issues in a later installment.  For now, let's proceed on.

Page 1: A Brief History of Objects
Page 2: COM is the Basis for Microsoft's Object Strategy
Page 3: Using Objects and Object Models
Page 4: Active Server Pages Objects
Page 5: Requests and Responses: An HTTP Perspective
Page 6: Using the Request Object
Page 7: A Change of Method
Page 8: Using Other Collections
Page 9: Tip of the Week and Summary

Author: Keith Cox
Date: 12/29/97

More articles about Active Server Pages
More articles by Keith Cox
Author Biography

write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.

body>