Our Sites:  Tutorial Buzz  |  How To Tree  |  Recipe Voice  |  Golf Twist  |  DIY Click  |  Movie Lizard  |  Halloween Twist  
Search:
Submit Link
Mail to a Friend
RSS FeedReceive updates via our RSS feed

Emailing Form Data with ASP

by: Amrit Hallan

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.

About the Author
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at amrit@bytesworth.com . For further details, visit http://www.bytesworth.com You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at bytesworth-subscribe@topica.com.

 

Home  |  News  |  Source Code  |  Tutorials  |  Components  |  Tools  |  Books  |  Free Magazines  |  Jobs  |  Gear  |  Hosting  |  Links
 
Copyright © 2000 - 2006 Code Beach  |    |  Privacy Policy
 
Free thumbnail preview by Thumbshots.org