ASP Lecture Series (15) Using HTML Forms
Author:Eve Cole
Update Time:2009-05-30 19:58:48
Using the ASP Request object, you can create a simple yet powerful script to collect and process HTML table data. In this topic, you will not only learn how to create a basic table processing script, but you will also gain some useful techniques for validating table information on the Web server and the user's browser.
About HTML tables
HTML tables, the most common method of collecting Web information, are special arrangements of HTML tags that provide user interface controls on a Web page. Text boxes, buttons, and check boxes are typical controls that allow users to interact with Web pages and submit information to the Web server.
The following HTML example generates a form in which the user can enter their name, age, and contains a button that submits this information to the Web server. The form also contains an implicit control (not displayed by the Web browser) that can be used to pass additional information to the Web browser.
<FORM METHOD="POST" ACTION="myfile.asp">
<INPUT TYPE="text" NAME="firstname">
<INPUT TYPE="text" NAME="lastname">
<INPUT TYPE="text" NAME="age">
<INPUT TYPE="hidden" NAME="userstatus" VALUE= "new">
<INPUT TYPE="submit" VALUE="Enter">
</FORM>
Forms that handle ASP input When the form submits information to the Web server, the user's Web browser requests the .asp file specified with the ACTION attribute of the HTML <FORM> tag (in the previous example, the file was called Myfile.asp ). .asp files contain scripts that manipulate table values, such as displaying a results table or querying information from a database.
There are three ways to collect values from HTML tables using .asp files
The static .htm file can contain a table whose values are sent to the .asp file.
An .asp file can create a form that mails its information to another .asp file.
The .asp file can create a form that mails its information to itself, the file that contains the form.
The first two methods operate in the same way, and when the form interacts with the gateway program, other than ASP, can include commands to read and respond to user selections.
Creating an .asp file that contains a table definition and sends information to itself is a more complex, but powerful way of working with tables. This process is demonstrated in Validating Form Inputs.
Get form input
The ASP Request object provides two collections that greatly simplify the task of retrieving form information attached to a URL request.
QueryString collection
The QueryString collection gets the value passed to the Web server as the text that follows the question mark in the requested URL. Table values can be appended to the requested URL by using the HTTP GET method or manually adding the table values to the URL.
For example, if the previous table example used the GET method (ACTION = "GET") and the user typed Jeff, Smith, and 30, the following URL request would be sent to the server:
http://scripts/Myfile.asp?firstname=Jeff&lastname=Smith&age=30&userstatus=new
Myfile.asp contains the following table processing script:
Hello, <%= Request.QueryString("firstname") %> <%= Request.QueryString("lastname") %>.
You are <%= Request.QueryString("age") %> years old.
<%
If Request.QueryString("userstatus") = "new user" then
Response.Write"This is your first visit to this Web site!"
End if
%>
In this case, the web server will return the following text to the user's web browser:
Hello, Jeff Smith. You are 30 years old. This is your first visit to this Web site!
The QueryString collection has an optional parameter that can be used to access one of multiple values that appears in the request body. You can also use the Count property to count the number of occurrences of a particular type of value.
For example, a form containing a multi-item listbox could submit the following request:
http://list.asp?food=apples&food=olives&food=bread
You can also use the following command to count multiple values:
Request.QueryString("food").Count
If you want to display multiple value types, List.asp should contain the following script;
<%Total = Request.QueryString("food").Count%>
<%For i = 1 to Total%>
<%= Request.QueryString("food")(i) %> <BR>
<%Next%>
The above script will display:
apples
olives
bread
Form collection When using the HTTP GET method to pass long and complex form values to the Web server, information may be lost. Most Web servers tend to tightly control the length of URL query strings so that lengthy table values passed using the GET method are truncated. If you need to send large amounts of information from a form to a Web server, you must use the HTTP POST method. This method is used to send table data in the HTTP request body, and the number of characters sent can be unlimited. You can also use the Form collection of the ASP Request object to retrieve values sent using the POST method.
The Form collection stores numeric values in the same way as the QueryString collection. For example, if a user populates a table with a long list of names, you can retrieve those names with the following script:
<% For i = 1 to Request.Form.Count %>
<% =Request.Form("names")(i) %>
<% Next %>
Validating form inputs A good form processing script should verify that the information entered into the form is valid before processing the data. Validation scripts can verify that the type of information users enter into a form is correct. For example, if your Web site contains a form that allows users to calculate financial information, before processing the results, you need to verify that the user actually entered numerical information and not text.
A very convenient way to validate form input is to create a form that passes information to itself. In this case, the .asp file contains a table from which the information can be obtained. For example, the following script verifies that the user entered a value in the "age" table field by passing information to itself:
<% If Isnumeric(Request.QueryString("Age")) then %>
<p>Hello, your age is <%=Request.QueryString("age")%>
<%Else%>
<p>Please enter a numerical age.
<%End If %>
<FORM METHOD= "POST" ACTION="verify.asp" >
Name: <INPUT TYPE="text" NAME="Name" >
Age: <INPUT TYPE="text" NAME="Age" >
<INPUT TYPE="submit" VALUE="Enter">
</FORM>
In this example, the script is also in the same Verify.asp file that contains the table. The form passes information to itself by specifying Verify.asp in the ACTION attribute.
You can also create client-side scripts to verify that users have entered valid information. Validating user input on a Web browser can reduce network traffic to the Web server, in addition to prompting users with form entry errors more quickly. The following script runs on the user's Web browser and verifies the user's information before submitting it to the Web server.
<SCRIPT LANGUAGE="VBScript">
<!--
Sub btnEnter_OnClick
DimTheForm
Set TheForm = Document.MyForm
If IsNumeric(TheForm.Age.Value) Then
TheForm.submit
Else
Msgbox "Please enter a numerical age."
End if
End Sub
//-->
</SCRIPT>
<FORM METHOD= "POST" NAME= MyForm ACTION="myfile.asp" >
Name: <INPUT TYPE="text" NAME="Name" >
Age: <INPUT TYPE="text" NAME="Age" >
<INPUT TYPE="button" NAME="btnEnter" VALUE="Enter">
</FORM>