ASP Lesson 6: HTML Forms - Submitting and Processing Data
One important use of ASPs is the processing of HTML Forms, which up until the creation of Active Server Pages has largely been done using CGI scripts, Perl, or some derivative thereof. ASP's perform faster than the old fashioned method of CGI because they run in the same process as the web server.
HTML Forms
Forms are generated from 5 basic objects, which are:
Form objects must be enclosed between <form></form> tags to be viewable in most browsers. A form follows the following construction:
<form method=post action=form_processing_script>
form objects...
</form>
Form objects contain some, or all, of the following properties:
The following form illustrates a basic form for gathering student information:
As you can see, Form1 is not very pleasing to the eye. We can make this form look better by encapsulating the form within a table. For starters, I'll create a simple table with labels on the left and form objects on the right:
Form 2 looks better than the first iteration, but it could still use some improvement. For instance, we could turn the borders off, add some back ground colors, nest some tables, or all of the above. You are, for the most part, only limited by your own imagination.
I've decided to stick with a fairly convential looking form to collect student data, which is illustrated below in Figure 6.3:
Processing results.
Now that we have the form, we need a method for processing the results. Before commiting the data to the database, it might be beneficial to present the data entered to the user. We are all human, and we all make mistakes. Better to allow the user to correct the data now before it's entered.
When a form gets submitted to an ASP script we use the REQUEST object to obtain the information contained in the form. Specifically, we use Request.Form(x), where x is either the index number or name of the item being requested. This returns a string containing the value of the form object. VBScript has one data type: Variant. Since variants can contain any type of data, when doing numerical comparisons it is best to explicitly convert your variable to a valid numerical data type (will be covered later).
The following example illustrates the Request and Response objects for processing our form data.
<html>
<head><title>View Form Results</title></head>
<body bgcolor="#FFFFFF">
Here's the data contained in the form:<p>
Student ID: <%response.write
Request.form("txtSID")%><br>
First Name: <%response.write
Request.Form("txtFName")%><br>
Last Name: <%response.write
Request.Form("txtLName")%><br>
Address: <%response.write
Request.Form("txtAddress")%><br>
City: <%response.write
Request.Form("txtCity")%><br>
State: <%response.write Request.Form("txtST")%><br>
Zipcode: <%response.write
Request.Form("txtZipcode")%>
</body>
</html>
Applying what you have learned.
Build an HTML form for entering student grades, then submit this form to a script that will output the contents of the form to the browser. The form should have the following fields:
Your form should have both Submit and Reset buttons, and text boxes for each of the four fields required above. Submit the form to a script called grades.asp.