spacer
Web Development Tutorials ASP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Emailing Form Data with ASP

By Amrit Hallan
2005-06-01


Emailing Form Data with ASP

Recently a client asked me to write a CGI program to handle the form output at his site. As usual, he wanted the form content, entered by the visitor, to be sent to him in a readable format.

As you have read above, my knowledge of Perl is not exactly enough to enable me to write serious CGI routines. I was about to refer him to a friend of mine, when I noticed (I haven't developed his site) that his site has been developed in ASP (no programming, just HTML pages saved as something.asp). Voila! Writing a form handling routine in ASP is cake walk. Here is how we do it.

We'll use the CDO (Collaborative Data Object) email object that comes with NT and other compliant systems. Don't get much bothered. You can always call up your web host and find out what email object is being used. The methodology, in ASP, is almost the same.

There are two methods actually. Preferred by advanced developers is the one in which we have the form and the processing code (the one that is mentioned with <form action="">) in the same file.

Suppose we have a file named formthings.asp. I leave the HTML things to your capable intellect, and get my hands dirty straightaway with ASP. At the onset of the ASP code (that we are going to write in VBScript), we check how the file is being called.

<%

if Request.ServerVariables("REQUEST_METHOD")="POST" then

do the coding.....

else

simply display the form, as if the page has been loaded for the first time.

end if

%>

This method is a bit complicated for a newbie, as there could be too many if_then_else_end_if nests. So what we're going to do is, we are going to divide the work in two files. The first one simply contains a form that executes the file that emails the form content to a specified email address.

Let the first file be form.asp that has the following form:

<form method="post" action="sendemail.asp">
<input type="text" size="15" name="name" /><br />
<input type="text" size="15" name="email" /><br />
<textarea row="5" col="15"
name="message"></textarea><br />
<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
</form>

You can see that this form calls "sendemail.asp", which contains the following code:

<% Dim sName, sEmail, sMessage, oSendMailer

sName=Request.Form("name")
sEmail=Request.Form("email")
sMessage="Request.Form("message")

'Comments:
'We store the form values in the local variables so that the server is not taxed if we have to access the values again and again.

Set oSendMailer=Server.CreateObject("CDONTS.NewMail")

'This creates a new variable that inherits all the features of CDONTS.

'Now we supply the required information to this variable.

oSendMailer.To="youremail@yourdomain.com"
oSendMailer.From=sEmail

'Remember sEmail is the visitor's email.

oSendMailer.Subject="Visitor's message from the web site."

oSendMailer.Body=sMessage

'sMessage contains the message entered by the visitor.

if oSendMailer.Send then
response.redirect "thankyoupage.asp"

'Keep in mind in order to use response.redirect,
'you have to add
'<% response.buffer=true %> just below
'<%@ language=vbscript %>

else
response.redirect "errorpage.asp"
end if

%>

Error could occur if you supply wrong values, or if there is a problem in the server.

So this is how you can email to you the form content from your web site.



Tutorial Pages:
» Emailing Form Data with ASP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Decision Making and Looping
» Arrays in ASP
» Sending HTML Email with ASP
» Dumping Form Information Onto a Web Page
» Installing Personal Web Server
» Your First ASP Page



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: