Did you find this useful?
Socialize it today.


ASP Form Collection

Sunday, September 02, 2012

tags ASP

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

ParameterDescription
variableSpecifies the name of the form element to retrieve values from.
indexSpecifies one of multiple values for a variable, or index of the parameters in the form.
countSpecifies 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 capture 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.

ExampleResults
Request.FormfirstName=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.Count2
Request.Form("firstName").Count1

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.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

ASP In A Nutshell Sams Teach Yourself Active Server Pages 3.0 in 21 Days Beginning ASP Databases