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:
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:
The following diagram 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.
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
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").
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).
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)
How ASP interprets a Query String
The QueryString collection is used to retrieve the variable values in the HTTP query string.
The HTTP query string is specified by the values following the question mark (?), like this:
Link with a query string
The line above generates a variable named txt with the value "this is a query string test".
Query strings are also generated by form submission, or by a user typing a query into the address bar of the browser.
Note: If you want to send large amounts of data (beyond 100 kb) the Request.QueryString cannot be used.
Syntax
Request.QueryString(variable)[(index)|.Count]
ASP QueryString Collection
The syntax for the QueryString collection is:
Request.QueryString(variable)[(index)|.Count]
As in the diagram, 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 diagram,
Request.QueryString("item").
Count would be 2.
The syntax for the `QueryString` collection in Classic ASP is still accurate. Here's a clarification: Key Points about `Request.QueryString` in Classic ASP:
Syntax:
Request.QueryString(variable)[(index)|.Count]
variable refers to the query string parameter name (e.g., item in your example).
(index) is optional and refers to a specific value if the query string parameter has multiple values.
.Count gives the number of values associated with the query string parameter.
Multiple Values:If a query string parameter appears multiple times in the URL, its values are stored as a collection (e.g., ?item=apple&item=banana). You can:
Use an index to access individual values:
Request.QueryString("item")(0) ' First value: "apple"
Request.QueryString("item")(1) ' Second value: "banana"
Use .Count to get the total number of values:
Request.QueryString("item").Count ' Returns 2
Single Value:If the query string parameter appears only once (e.g., ?item=apple), Request.QueryString("item") directly returns the value "apple". However, .Count in this case will return 1.
Note on Cookies: The Request object in Classic ASP can also be used to read browser cookies:
Request.Cookies("cookieName")
This allows you to access cookies sent by the browser. The behavior described in your example is consistent with how Classic ASP processes query string collections.
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