ASP Reading Form Data
Learn how to read data submitted from HTML forms in classic ASP, using Request.Form for POST submissions and Request.QueryString for GET or URL parameters.
Two Ways Data Reaches Your ASP Page
An HTML form sends its data to the server in one of two ways, depending on its method attribute. A form with method="get" appends the field values onto the URL as a query string, while a form with method="post" sends them inside the body of the request, invisible in the address bar. ASP gives you a separate collection for reading each kind, and using the wrong one is one of the most common beginner mistakes.
Reading POST Data with Request.Form
Example: Reading a posted signup form
<%
Dim userName, userEmail
If Request.Form("submitted") = "yes" Then
userName = Request.Form("username")
userEmail = Request.Form("email")
Response.Write("Thanks for signing up, " & userName & "!<br>")
Response.Write("We will send updates to " & userEmail & ".")
End If
%>
<form method="post" action="signup.asp">
<input type="hidden" name="submitted" value="yes">
<input type="text" name="username" placeholder="Your name">
<input type="text" name="email" placeholder="Your email">
<input type="submit" value="Sign Up">
</form>Reading GET Data with Request.QueryString
Example: Reading parameters from the URL
<%
Dim productId, sortOrder
productId = Request.QueryString("id")
sortOrder = Request.QueryString("sort")
Response.Write("Showing product #" & productId & "<br>")
Response.Write("Sorted by: " & sortOrder)
%>
<!-- Visiting product.asp?id=42&sort=price would print: -->
<!-- Showing product #42 -->
<!-- Sorted by: price -->- If a field was not submitted at all, Request.Form("field") or Request.QueryString("field") returns an empty string rather than raising an error.
- It is good practice to check Request.Form("field") <> "" before using a value, in case the visitor left it blank.
- When a form field can hold more than one value, such as a group of checkboxes sharing the same name, Request.Form("field").Count tells you how many values were submitted.
Example: Reading multiple checkbox values
<%
Dim i, colorCount
colorCount = Request.Form("colors").Count
Response.Write("You selected " & colorCount & " colors:<br>")
For i = 1 To colorCount
Response.Write(Request.Form("colors")(i) & "<br>")
Next
%>Exercise: ASP Forms
When an HTML form uses method="post", how does the ASP page read the submitted field values?