Putting It All Together and Wrapping Up
July 31, 2000
We have come a long way since the beginning of this tutorial. If you have
successfully made it this far then pat yourself on the back because you
have learned a lot. Once you get some practice, you will be able to take
this fairly simple concept, some creativity and some elbow-grease to create
very intuitive and complex dynamic pages.
Below you will find the complete source code (for a working "live" version
of the code,
visit this test page I set up at
Enfused). Following the code is
explanation that should tie up any loose ends and some enhancements you
may want to consider. As an additional note, I've removed the email
addresses for the CC and BCC code in the example because we do not want
to accidentally spam someone in case those dummy addresses I cooked up
were real.
<%
if Request.Querystring("isSubmitted") = "yes" then
Dim fname, lname
Dim objCDO
fname = Request.Querystring("First_Name")
lname = Request.Querystring("Last_Name")
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "corinth@enfused.com"
objCDO.To = "corinth@enfused.com"
objCDO.Cc = ""
objCDO.Bcc = ""
objCDO.Subject = "Submitted form data from my page"
objCDO.Body = "Name: " & fname & " " & lname
objCDO.BodyFormat = 1
objCDO.MailFormat = 1
objCDO.Send
ConfirmMsg = "Thanks for submitting your name!"
end if
%>
<html>
<head>
<title>ASP Form Sample</title>
</head>
<body>
<% if ConfirmMsg <> "" then %>
<h2><%= ConfirmMsg %></h2>
<% end if %>
<form action="formtest.asp" method="get" name="Input_Form">
First Name:
<input type="text" size="30"
maxlength="50" name="First_Name">
<br>
Last Name:
<input type="text" size="30"
maxlength="50" name="Last_Name">
<br>
<input type="hidden" name="isSubmitted" value="yes">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
The first major change you are likely to notice is the extra code dealing
with ConfirmMsg in the HTML portion of the page. As
promised, I will explain.
<% if ConfirmMsg <> "" then %>
<h2><%= ConfirmMsg %></h2>
<% end if %>
Using another conditional test, the page checks to see if
ConfirmMsg is not a zero-length string (or is not
empty). If ConfirmMsg is empty that means
the ASP form handling code did not execute and assign "Thanks for
submitting your name!" to ConfirmMsg and will
therefore not write anything to the page. However, if
ConfirmMsg is not empty, that means the ASP
code was executed and it should display the message.
Another item of note is that one may use one line of ASP to initiate a
conditional test or loop, followed by HTML and then followed with ASP
again. The ability to "sandwich" HTML in between looping or condition
testing becomes more important as the complexity of the HTML to be
written on every loop or test increases. Also, note that the
<%= ConfirmMsg %> is a special form of ASP code.
When the <% is followed by an equals sign (=) then
the ASP processor acts in an output mode, allowing the designer to use a
variable name without typing response.write(varName).
The possibilities for enhancement here are endless. One possibility,
that I asked you to try earlier, is to allow users to enter their own
email addresses for the From attribute of CDONTS.
To do it, first add a field to your form like:
Email Address:
<input type = "text"
size = "30"
maxlength = "50"
name = "Email_Address">
Then, using your ability to assign variables (and still assuming the
get method is being used) add a variable for the email
address, like so:
EmailVar = Request.QueryString("Email_Address")
Finally, change the CDONTS code to the following:
objCDO.From = EmailVar
You can do the same with any of the CDO variables, depending on your needs.
At Enfused we designed a special
"Recommend to a Friend"
page that uses these same exact techniques, allowing the user to send mail
from our site with whatever values they want.
Another possibility includes writing the submitted form data to a database,
like we did on
Enfused's Suggest a Site page. With
only a little extra coding and some SQL know-how, doing this can be quite
easy because ASP has a special database-connectivity object, called ADO, to
make your life easier. For some more info about working with ADO, check
out this
recent WDVL article on the subject.
In this tutorial, we learned about Microsoft's Active Server Page technology
and how to use it along with the NT's Collaboration Data Object (CDONTS),
to create a page that will email user-submitted form data. Forms drive
interactivity and the Internet, and correctly harnessed, will allow you
to make the best web sites possible. With these techniques and some
practice, the sky is the limit on what you can do.
Working with CDONTS
Using ASP for Form Handling
Using ASP for Form Handling: Part 2 - Server Side Form-Field Validation
|