Even Smarter Forms - Page 2
September 5, 2000
If you remember the
first article in this series
then you will remember how we took submitted form data and stored it in
variables as follows:
<%
Dim fname, lname
fname = Request.Form("First_Name")
lname = Request.Form("Last_Name")
%>
Assuming the
HTML form is using
method="post" the, above code takes
First_Name and Last_Name in the Forms Collection
and stores them in fname and lname respectively.
To take it a step further, recall that using a conditional statement, we
ensured that the ASP code only executed when the form was submitted and a
condition was met. When taken as a whole, it becomes very easy to make a
form have persistent memory. If you are confused about what giving a form
memory means, it is actually a rather simple concept that you probably see
everyday on the Web. Have you ever completed a page of a multi-page
form, submitted it, realized you made a mistake on page two, and wanted to
go back and fix it? You and everyone else! Well designed sites will save
the information you entered in the form until you are completely finished
entering the relevant data. All that you need to do in your own web page
is add some simple code to the form-field tag. Below is an example of how
this would be accomplished with the First_Name above.
<form action="formtest.asp" method="post">
...
...
<input type="text" name="First_Name" value="<%=fname%>">
</form>
Through the use of the value attribute, ASP allows us to
literally complete the HTML code before it reaches the client using
server-side variables. The great thing about VBScript and ASP in this
instance, is that variables do not need to be defined in order to return a
value. This fact is important when the page is accessed for the first time
before the form is submitted. In such a case, fname will just
be empty and value will end up looking like
value="" to the client and the form-field will be blank.
After the form is submitted for the first time, the server will
automatically insert the value of fname in the value, giving
your form simple memory!
|
NOTE:
There are other ways to give forms memory. The method we are
using will only last as long as the user keeps the browser open
and pointed to our site. If we need to save form-field
information for later use, we need a more robust and persistent
solution. One possibility is to use cookies to store the
relevant values. Then, instead of storing submitted form-field
data in variables, one would store data taken directly from a
Cookies (which in ASP has a collection just like Forms). The
input tag would look exactly the same. This method is
particularly useful for shopping carts or storing payment
information on an e-Commerce site like
Buy.com.
|
To achieve the same effect with a select or
checkbox (or any other potentially multi-valued field) you
need to do a little more advanced processing. For example, the following
code will give a field for "State" memory:
<% state = Request.Form("state") %>
<select name="state">
<option value="noneSelected">- Select from List -</option>
<option value="Alabama"
<% if state="Alabama" then response.write("SELECTED") %>>
Alabama - AL</option>
<option value="Alaska"
<% if state="Alaska" then response.write("SELECTED") %>>
Alaska - AK</option>
<option value="Arizona"
<% if state="Arizona" then response.write("SELECTED") %>>
Arizona - AZ</option>
<option value="Arkansas"
<% if state="Arkansas" then response.write("SELECTED") %>>
Arkansas - AR</option>
...
...
<option value="Wyoming"
<% if state="Wyoming" then response.write("SELECTED") %>>
Wyoming - WY</option>
</select>
Now that you understand how to give your form memory, the first thing to
do is go back to your form and implement this functionality now,
before doing anything else. This step will be time-consuming to you as a
developer, but will make your visitors happier, and that is (should be)
a primarily goal of all web developers. Once you have done that, move on
to the next page and we will begin talking about actually validating the
submitted form data.
Using ASP for Form Handling: Part 2 - Server Side Form-Field Validation
Using ASP for Form Handling: Part 2 - Server Side Form-Field Validation
Dealing with Blank Fields - Page 3
|