HOW TO: Use a Generic Collection to Parse HTTP Form Data with ASP

Article translations Article translations
Article ID: 254742 - View products that this article applies to.
This article was previously published under Q254742
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:
http://www.microsoft.com/technet/security/prodtech/IIS.mspx
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to parse form data easily when you use Microsoft Active Server Pages (ASP) by using a generic collection instead of by explicitly defining the ASP collection for GET or POST requests.

When you use ASP to process data that is entered on a Web page through a form, two different collections are used depending on which HTTP method is specified in the form declaration:
  • GET uses the Request.QueryString collection.
  • POST uses the Request.Form collection.
This article describes how to process form data regardless of which HTTP method is used on the form.

NOTE: You may receive an error message if you copy the examples directly from this article to Microsoft FrontPage. The angle brackets (< and >) may appear as escaped HTML code (< and >). To work around this behavior, paste the script in a blank Notepad document, and then copy it from Notepad before you paste it in FrontPage.

Create the Sample Pages

The following steps describe how to create a sample HTML page with two Web forms (one that uses the GET method and one that uses the POST method), and how to create a sample ASP page that processes the form by using either method.
  1. Save the following HTML code as Getdata.htm in the root folder of an IIS Web site:
    <html>
    <body>
    POST <form action="showdata.asp" method="POST">
    <input type="text" name="txtData">
    <input type="submit">
    </form>
    <br>
    GET <form action="showdata.asp" method="GET">
    <input type="text" name="txtData">
    <input type="submit">
    </form>
    </body>
    <html>
    					
  2. Save the following ASP code as Showdata.asp in the root folder of an IIS Web site:
    <% @language="vbscript" %>
    <html>
    <body>
    <%
       ' Declare a variable for the generic collection.
       Dim objRequest
       ' Determine the HTTP method used.
       If UCase(Request.ServerVariables("HTTP_METHOD")) = "GET" Then
          ' Use the QUERYSTRING collection with GET requests
          Set objRequest = Request.QueryString
       Else
          ' Use the FORM collection with POST requests.
          Set objRequest = Request.Form   End If
       ' Escape and output the form data. 
       Response.Write Server.HTMLEncode(objRequest("txtData"))   
    %>
    </body>
    <html>
    					
When you open Getdata.htm by using HTTP (that is, by using an address such as http://localhost/getdata.htm), you may use either form to view the form results.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
300104 HOW TO: Pass Form Data from One Page To Another by Using GET and POST in Active Server Pages

Properties

Article ID: 254742 - Last Review: June 23, 2005 - Revision: 3.2
APPLIES TO
  • Microsoft Active Server Pages 4.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
  • Microsoft Internet Information Services version 5.1
Keywords: 
kbhowto kbhowtomaster KB254742

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com