ASP   «Prev  Next»

Lesson 6Request.QueryString
ObjectivePass form data as part of a URL request.

ASP Request.QueryString

If you have ever noticed a long URL with a question mark (?) in the middle while you are at a Web site, you have probably seen an example of a query string. Query strings contain user data appended to a URL. Unlike data in browser cookies, the data in a query string can't be stored between user visits to a Web site, but they are useful for retaining user information during a single visit.

Using GETs and POSTs

The METHOD field of an HTML FORM command allows the use of either POST or GET for its method, and requests the URL in the ACTION field (in our case, an ASP script) from the server. Each method is useful, but used differently.
POST is used for passing more complicated information or information to be processed (in a situation, for example, where a user clicking the browser's Reload button could result in an accidental duplicate order). In the previous lesson, we used Request.Form to read data submitted with the POST method.
GET is typically used to pass parameters for retrieving data, and can be used to pass hidden fields as well, such as a user's current location in a Web application. Using the GET method encodes the Form data and appends it to the end of the ACTION URL. A ? separator follows the URL; other form fields are separated from each other by a &. character.

How ASP interprets a query string

The official specification for URL syntax breaks down URLs as follows:

http://<host>:<port>/<path>?<searchpart>&<searchpart2...>

The searchpart is the query string, and is often used to pass search information, shopping cart, and user login information. A typical query string for an online store order might look like this:

http://www.tsmania.com/order.asp?user=3433&item=23A6&item=33A5 

The following MouseOver illustrates how the query string from the URL will be interpreted by the ASP Request object and how each piece of data can be referenced.

  1. www.tsmania.com/order.asp?user=3433&item=23A6&item=333A5
  2. Blue: This is the Action URL from the HTML GET command. The order.asp file will be opened and the data following the ? symbol will be read and stored
  3. Orange:This piece of data identifies the user by his or her ID, in many Web sites, this will be used to retrieve the user's database record. It can be individually referenced as QueryString("user").
  4. Green: This piece of data is the first of two items that this use is ordering. It can be individually referenced as QueryString("Item")(1).
  5. Purple: This piece of data is the second of two items that this use is ordering. It can be individually referenced as QueryString ("Item") (2)

Request Query String

ASP QueryString Collection

The syntax for the QueryString collection is:
Request.QueryString(variable)[(index)|.Count] 

As in the MouseOver example, you can specify an index to refer to a specific element within a multiple-value query variable, or you can reference the total number of array elements in a variable. For the example in the MouseOver, Request.QueryString("item"). Count would be 2.
The next lesson describes how the Request object can read a browser cookie.

ASP Request - Exercise

Click the Exercise link below to use ASP to pass data in a query string.
ASP Request - Exercise