ASP Forms and User Input

The Request.QueryString and Request.Form commands can be used to retrieve information from a form, such as user input.

Example:

Form Using method="get"
How to interact with users using the Request.QueryString command.
Form Using method="post"
How to interact with users using the Request.Form command.
Form Using Radio Buttons
How to interact with users using Request.Form through radio buttons.

User Input

The Request object can be used to retrieve user information from a form.

HTML Form Instance

<form method="get" action="simpleform.asp">
<p>First Name: <input type="text" name="fname" /></p>
<p>Last Name: <input type="text" name="lname" /></p>
<input type="submit" value="Submit" />
</form>

Information entered by the user can be retrieved in two ways: Request.QueryString or Request.Form.

Request.QueryString

The Request.QueryString command is used to collect values from a form that uses method="get". Information sent from the form using the GET method is visible to all users (appears in the browser's address bar) and there is a limit to the amount of information sent.

HTML Form Instance

<form method="get" action="simpleform.asp"
<p>First Name: <input type="text" name="fname" /></p>
<p>Last Name: <input type="text" name="lname" /></p>
<input type="submit" value="Submit" />
</form>

If the user enters "Bill" and "Gates" in the form instance above, the URL sent to the server will be similar to the following:

http://www.codew3c.com/simpleform.asp?fname=Bill&lname=Gates

Assuming the ASP file "simpleform.asp" contains the following code:

<body>
Welcome
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
</body>

The browser will display the following:

Welcome Bill Gates

Request.Form

The Request.Form command is used to collect values from a form that uses the "post" method. Information sent from the form using the POST method is not visible to the user and there is no limit to the amount of information sent.

HTML Form Instance

<form method="post" action="simpleform.asp"
<p>First Name: <input type="text" name="fname" /></p>
<p>Last Name: <input type="text" name="lname" /></p>
<input type="submit" value="Submit" />
</form>

If the user enters "Bill" and "Gates" in the form instance above, the URL sent to the server will be similar to the following:

http://www.codew3c.com/simpleform.asp

Assuming the ASP file "simpleform.asp" contains the following code:

<body>
Welcome
<%
response.write(request.form("fname"))
response.write(" " & request.form("lname"))
%>
</body>

The browser will display the following:

Welcome Bill Gates

Form Validation

Data entered by the user should be validated whenever possible (through client-side scripts). Client-side validation is faster and can reduce the load on the server.

If user data will be entered into the database, you should consider using server-side validation. A good way to validate a form on the server is to return the (validated) form to the form page instead of redirecting to a different page. The user can then receive error information on the same page. This makes it easier for users to find errors.