Collecting and processing form information

In this section, you will learn how to use the Request object for collecting form data from the browser. Forms are popular way of collecting information from the user.

What are forms?

A form is a web document used for collecting information from the user. For us to collect any information from the user, basically we need to:

  1. create and display the form
  2. have the user submit the form to the server (i.e., by clicking on the "Submit" button)
  3. process the submitted form data

When we work with forms or any other means to collect information from the user, we cannot assume that the user will enter correct data. If the user enters invalid data, we need to somehow programmatically catch that. The form validation is normally done with client-side scripts (such as JavaScript, VBScript). For this section, we will assume that the user will enter valid data so we won't be doing any data validation.

A form is created with HTML <form> tag. We will assume here that you know how to create a form and decide which form elements (such as text boxes, check boxes, radio buttons, selection list, etc.) to use to collect the information you desire from the user. It is very important to choose the appropriate form field type when creating a form. For instance, choose text boxes to obtain numerical (phone number, street number, etc.) or string (person's name or title, company name, etc.) input. Display choices for known and common inputs (i.e., listing names of countries or states versus asking the user to enter them into a text field) to avoid typos and incorrect data.

The difference between GET and POST

There are two methods used for submitting information to the server:

  1. GET
  2. POST

The GET method is also referred to as querystring. In this method, information to the server is sent as variableName = Value and the is added to the URL of the form. The querystring starts with a question mark (?) and each variable is separated by an ampersand (&). The variable here is the name that is used in the form element. For example, if your text box is named "first_name" and when the user submits the form with that field with the value "John", this is what is submitted to the server: URL/?first_name=John.

In the POST method, the data that the user enters and submits is not added to the end of URL.

The main advantage of the GET method is that the user could bookmark the results of a processed form to eliminate the need for reentry of the same data. The main advantage of the POST method, however, is that it does not display personal data (user name or passwords, etc.) in the URLs. Thus this keeps URLs short. Generally speaking, if you are collecting a large amount of data or personal data from the user you should consider using the POST method.

If you are using the GET method for your form, use the request.QueryString() method to collect the form data. For the POST method, use the request.Form() method. Click on the appropriate link below to learn more about retrieving values for a particular form element:

HTML form method: GET POST
ASP object to use Request.QueryString () Request.Form ()
Type of form field to process Text box Text box
  Password box