Using forms in your ASP Applications is a common way of collecting user input for further processing. The ASP Form collection retrieves the values of form elements posted to the HTTP request body, using the POST
method. Unlike variables that are passed in a query string using the GET
method, form input is passed in the Request
headers.
Syntax
Request.Form(element)[(index)|.Count]
Parameters
Parameter | Description |
---|---|
variable | Specifies the name of the form element to retrieve values from. |
index | Specifies one of the multiple values for a variable, or index of the parameters in the form. |
count | Specifies the number of multiple values for a form element. |
Examples
Consider the following HTML page which contains a form to collect data.
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<form action="results.asp" method="post">
<p>First name: <input name="firstName"></p>
<p>Pick one or more colors:</p>
<select name="color" multiple="multiple">
<option>Red</option>
<option>Orange</option>
<option>Yellow</option>
<option>Green</option>
<option>Blue</option>
<option>Purple</option>
</select>
<p><input type="submit"></p>
</form>
</body>
</html>
On the target page, results.asp
, you can use the Request.Form
Collection to collect the information passed in the Request
headers.
In the following example, if you were interested in looping through all of the form elements and capturing the associated values, you can use this sample code:
<%
For x = 1 to Request.Form.Count
Response.Write(Request.Form.Key(x) & " = ")
Response.Write(Request.Form.Item(x) & "<br />")
Next
%>
The results may look like this:
firstName = John
color = Red, Blue
Here are some additional examples, and the associated result.
Example | Results |
---|---|
Request.Form | firstName=John&color;=Red&color;=Blue |
Request.Form(2) | Red , Blue |
Request.Form("color") | Red , Blue |
Request.Form.Key(1) | firstName |
Request.Form.Item(1) | John |
Request.Form.Count | 2 |
Request.Form("firstName").Count | 1 |
Once you understand how to access the variable or variables submitted using a form, the next step is to determine what to do with those values. You may use that information to look up product information stored in a database, or send an email message to a user.