How To Pass Form Data from One Page To Another by Using GET and POST in Active Server Pages (300104)



The information in this article applies to:

  • Microsoft Active Server Pages

This article was previously published under Q300104

SUMMARY

One of the most commonly performed actions in Active Server Pages (ASP) Web development is passing data from one page to another for processing. This article contains step-by-step examples to demonstrate how to do this. Two techniques are involved: one uses the POST method, and the other uses the GET method. Although there are deeper levels of complexity regarding these approaches that you may consider, this article just illustrates how to apply these ideas.

NOTE: In these samples, only the second pages in each sequence must be .asp pages, because only these pages actually contain server-side code for the demonstration. The initial pages can be either .htm or .asp pages.

back to the top

Using the POST Method

When you use the POST method, you send the values for the HTML controls in a form to the page that is specified in the action attribute. To do this, follow these steps:
  1. Create a new .htm page and name it Gatherdata_post.htm. To do this in Visual InterDev version 6.0, right-click the project node in the Project Explorer, click Add, click HTML Page, type Gatherdata_post.htm as the name of the page, and then click Open.
  2. Paste the following code in the new page:NOTE: You should clear out the default code that InterDev adds when you initially create the page before you paste this code in the page. Otherwise, you will have redundant tags.
    <html>
    <head>
    </head>
    <body>
    <FORM action="RetrieveData_Post.asp" id=form1 method=post name=form1>
    	First Name:
    	<br>
    	<INPUT id="txtFirstName" name="txtFirstName" >
    	<br>
    	Last Name:
    	<br>
    	<INPUT id="txtLastName" name="txtLastName" >
    	<br>
    	<INPUT type="submit" value="Submit"> 
    </FORM>
    </body>
    </html>
    					
  3. Save the page. To do this, click Save Gatherdata_post.htm on the File menu in the Visual InterDev IDE.
  4. Create another page named Retrievedata_post.asp. Paste the following code in the page, and save the page:
    <%@ Language=VBScript %>
    <html>
    <head>
    </head>
    <body>
    <%
    Response.Write("First Name: " & Request.Form("txtFirstName") & "<br>")
    Response.Write("Last Name: " & Request.Form("txtLastName") & "<br>")
    %>
    </body>
    </html>
    
    					
  5. Run Gatherdata_post.htm by right-clicking the page and selecting View in browser. Enter some values for the first and last names, and then click Submit. Note that the data that you entered in Getdata_post.htm has been sent through HTTP POST to the Retrievedata_post.asp page for retrieval using the Request.Form collection.
back to the top

Using the GET Method

When you use the GET method, you send the values for the HTML controls in a form to the page that is specified in the action attribute in a manner that is similar to the POST approach that was described previously. However, you will notice that the values are sent in the QueryString. One main issue to consider when you use GET instead of POST is that the values are visible in the URL.

To use the GET method, follow these steps:
  1. Create a new .htm page and name it Gatherdata_get.htm. To do this in Visual InterDev version 6.0, right-click the project node in the Project Explorer, click Add, click HTML Page, type Gatherdata_get.htm as the name of the page, and then click Open.
  2. Paste the following code in the new page:NOTE: You should clear out the default code that InterDev adds when you initially create the page before you paste this code in the page. Otherwise, you will have redundant tags.
    <html>
    <head>
    </head>
    <body>
    <FORM action="RetrieveData_Get.asp" id=form1 method=GET name=form1>
    	First Name:
    	<br>
    	<INPUT id="txtFirstName" name="txtFirstName" >
    	<br>
    	Last Name:
    	<br>
    	<INPUT id="txtLastName" name="txtLastName" >
    	<br>
    	<INPUT type="submit" value="Submit" id=submit1 name=submit1> 
    </FORM>
    </body>
    </html>
    					
  3. Save the page. To do this, click Save Gatherdata_get.htm on the File menu in the Visual InterDev IDE.
  4. Create another page named Retrievedata_get.asp. Paste the following code in the page, and save the page:
    <%@ Language=VBScript %>
    <html>
    <head>
    </head>
    <body>
    <%
    Response.Write("First Name: " & Request.QueryString("txtFirstName") & "<br>")
    Response.Write("Last Name: " & Request.QueryString("txtLastName") & "<br>")
    %>
    </body>
    </html>
    					
  5. Run Gatherdata_get.htm by right-clicking the page and selecting View in browser. Enter some values for the first and last names, and then click Submit. Note that the data that is entered in Getdata_get.htm is sent through GET to the Retrievedata_get.asp page for retrieval using the Request.QueryString collection. As you have seen, the QueryString collection is where your data is made available to the retrieving ASP page when you use GET as the action for the sending page, and the Form collection is used to retrieve the data when you implement POST.
back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

254742 IIS: ASP Parsing of HTTP Form Data Using a Generic Collection

208427 INFO: Maximum URL Length Is 2,083 Characters in Internet Explorer

254786 PRB: Query String Truncated

back to the top

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbASPObj kbCodeSnippet kbhowto kbHOWTOmaster kbSample kbScript KB300104 kbAudDeveloper