Getting Information from a User

Often you want to get information about a user, for example, the type of browser the user is running. You might also want to get information from a user, for example, when the user submits information in forms. The ASP built-in object Request makes getting this information easy.

The Request object gives you access to any information that is passed into a script with an HTTP request. This includes:

The Request object has four associated collections:

You can use the following syntax to access the information in the Request object:

Request.CollectionName(variablename)

CollectionName is either QueryString, Form, Cookies, or ServerVariables. VariableName is the name of the variable in the collection that you want to access.

You can also access the variables without including the collection name:

Request(variablename)

The collections are searched in this order: QueryString, Form, Cookies, ServerVariables. The first variable that matches variablename is returned.

Note   If an HTML page might have more than one variable with the same name, make sure you include the collection name between Request and the variable name.

For a more detailed description of the Request object and its collections, refer to Object Reference.

Getting Information From HTML Forms

The form is the most frequently used medium for getting information from a Web user. A form’s text boxes, option buttons, and check boxes, displayed on an HTML page in a browser, provide the user an easy way of submitting information. When the user clicks the Submit button, the browser sends the collected information to the Web server.

You can use .asp files to collect or process form values in three ways:

The first two methods operate in basically the same way as forms that interact with other gateway programs, except that Active Server Pages makes it easier for you to write commands to read and respond to the user choices.

Writing an .asp file that contains a form definition that posts the information back to itself is a slightly more complicated but very powerful means of working with forms. This process is discussed later in this section.

Using the QueryString Collection

Although you could use the QUERY_STRING Server Variable to pass the unparsed QUERY_STRING information along with a user request, ASP provides a QueryString collection to make this information readily available. The QueryString collection contains all the information passed as a parameter after a question mark in a URL or from a form if the METHOD is GET rather than POST.

For example, when a user sends the following URL request, the Request.QueryString collection would contain two members: name and age.

<A HREF="myasp.asp?name=William+James&age=30"> 

The following script uses the Request object to access these values.

Welcome, <% =Request.QueryString("name") %>. 
Your age is <% =Request.QueryString("age") %>. 

In this case, the following text would be sent back to the user:

Welcome, William James. Your age is 30. 

The QueryString collection also automatically handles the case of multiple variables with the same name. When parsing a query string such as name=Cameron&name=James&name=Seth for example, a new collection called name is created that in turn contains three values: Cameron, James, and Seth. Each of these values is indexed by an integer, with the following results:

Reference Value
Request.QueryString("name")(1) Cameron
Request.QueryString("name")(2) James
Request.QueryString("name")(3) Seth

A collection built like this also supports a Count property that describes how many items are in the collection. In this example, Request.QueryString("name").Count = 3, because there are three separate values stored in the name collection.

If the HTML uses the variable normally, without treating it as a collection, then it will become a comma-delimited string. Following the above example, the value of Request.QueryString("name") is "Cameron, James, Seth".

Using the Form Collection

The Form collection contains all the values that a user entered in the controls on a form and submitted with the POST method. For example, when the user submits the following form:

<form action="/scripts/submit.asp" method="post">
<p>Your first name: <input name="firstname" size=48>
<p>What is your favorite ice cream flavor: <select name="flavor">
<option>Vanilla <option>Strawberry <option>Chocolate <option>Rocky Road 
</select>
<p><input type=submit>
</form>
 

The following request might be sent:

firstname=James&flavor=Rocky+Road 

And the following script:

Welcome, <% =Request.Form("firstname") %>.
Your favorite flavor is <% =Request.Form("flavor") %>. 

Would result in the following output:

The Form collection treats multiple parameters with the same name in the same way that the QueryString collection does.

Using the ServerVariables Collection

The ServerVariables collection provides information from the HTTP headers that are passed along with a user’s request. Some of this information, such as the SERVER_PORT server variable defined by the Common Gateway Interface (CGI) standard, might be of limited value:

This HTTP request was received on 
  TCP/IP port <% = Request("SERVER_PORT") %>. 

Other information, such as that contained in the HTTP_USER_AGENT header variable can be used to provide customized responses to users:

<% language = Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") 
If language = "en" Then %> 
<!--#INCLUDE FILE="/myapp/Englishpage.asp"--> 
<% Else %> 
<!--#INCLUDE FILE="/myapp/Otherlang.asp"--> 
<% End If %> 

Posting Input to the Originating ASP page

ActiveX Server gives you the flexibility to define a form in an .asp file that posts its input values back to the originating .asp file. When a user submits the values in a form to your .asp file, you can use the Request object to read these values. If you receive an invalid value, you can send a message back to the user, pointing out the problem and asking for a different value.

If you send only a message, the user has to go back to the page with the form. You can save the user this step by sending your message and defining the form again.

If you post form input messages to the same file that originally defines the form, however, you can send informational messages along with the content of the form and you only have to define the form once.

When you have received information from a user, you may want to make sure it is valid according to your criteria. For example, the following form definition allows a user to submit his or her e-mail address. If the value does not contain @, it is probably incomplete. The following script in GetEmail.asp checks for this.

<HTML> 
<!-- This is GetEmail.asp --> 

<% 
  If IsEmpty(Request("Email")) Then 
    Msg = "Please enter your email address." 
  ElseIf InStr(Request("Email"), "@") = 0 Then 
    Msg = "Please enter an email address" & _
    " in the form username@location."
  Else
    Msg = "This script could process the " & _
    "valid Email address now."
  End If

%> <FORM METHOD="POST" ACTION="GetEmail.asp">
<PRE>
Email: <INPUT TYPE="TEXT" NAME="Email" SIZE=30 VALUE="<%= Request("Email") 
%> ">
<% =Msg %> <P> 
<INPUT TYPE="SUBMIT" VALUE="Submit">
</PRE>
</FORM>
</HTML>

Using the Cookies Collection

A cookie is an identification token sent to a client browser that may or may not be stored on the client’s hard drive, depending on the browser’s capablilties. Cookies allow a set of information to be associated with a single user during many URL requests in a Web application. ASP scripts can both get and set the value of a cookie by using the Cookies collection.

To get the value of a cookie, use Request.Cookies. For example, if the client HTTP request sets food=desert, then the following statement retrieves the value of cookie-type:

<% =Request.Cookies("food") %> 

If an HTTP request sets multiple values to the same cookie, ASP creates an indexed cookie. Each value is assigned a key; you can retrieve a particular cookie key value by using the syntax Request.Cookies(“name”)(“key”). For example, if a client sends the following HTTP request:

food=desert=cake&cake=chocolate

The following script command returns the value chocolate:

<% =Request.Cookies("food")("cake") %> 

To set the value of a cookie, use Response.Cookies. If the cookie does not already exist, Response.Cookies creates a new one:

<% Response.Cookies("food")="dinner" %> 

Similarly, to set the value of a cookie key:

<% Response.Cookies("food")("dressing")="Caesar" %> 

If an existing cookie has key values but the Response.Cookies method does not specify a key name, then the existing key values are deleted. Similarly, if an existing cookie does not have key values but the Response.Cookies method specifies key names and values, the existing value of the cookie is deleted and new key-value pairs are created.


© 1996 Microsoft Corporation. All rights reserved.