Friday, March 29, 2024

Web Developer Class: Using Forms for Feedback or Contact Pages

Want to save information from a form to a file or email it to yourself? You can’t just use HTML for this–you need a server side language. There are many such languages that you can use, like PHP, ASP, ASP.net, CGI-Perl, JSP, ColdFusion, ect. We’ll take a look at some potential solutions in this web developer class on using forms.

First, find out what your web host supports and then you can learn the appropriate language. For any form action you need a server side technology. The most common is a form mailer–this takes the results of a form and mails it to the address specified. Not all servers/hosting support server side technologies, but there are many free form mailers available on the net for you to use.

Using an External Script Service

I recommend using the following script if your server does not support a server side technology. Dont forget to replace the email and thankyou page with your own email address and URL.

<form action="http://php.richardturner.com/mailer.php" method="post">
<p>
<input type="hidden" name="form_email" value="YOUR_EMAIL@DOMAIN" /><br />
<input type="hidden" name="form_thankyou" value="http://www.YOURSITE.com/THANKYOUPAGE.HTML" /><br />
Your Form Here<br />
<input type="submit" value="Submit" />
</p>
</form>

Using a PHP Form Mailer

You can do the same thing using your own form handler. Here’s a simple PHP script that will do just that:

<?PHP

#######################################################
# This script is Copyright 2003, Infinity Web Design  #
# Distributed by [url]http://www.webdevfaqs.com[/url]            #
# Written by Ryan Brill                               #
# All Rights Reserved – Do not remove this notice     #
#######################################################

## The lines below need to be edited…

###################### Set up the following variables ######################
#
$to = “you@your.com”; #set address to send form to
$subject = “Results from your Request Info form”; #set the subject line
$headers = “From: Form Mailer”; #set the from address, or any other headers
$forward = 0; # redirect? 1 : yes || 0 : no
$location = “thankyou.htm”; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################

## set up the time ##

$date = date (“l, F jS, Y”);
$time = date (“h:i A”);

## mail the message ##

$msg = “Below is the result of your feedback form. It was submitted on $date at $time.nn”;

if ($_SERVER[‘REQUEST_METHOD’] == “POST”) {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) .” : “. $value . “n”;
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) .” : “. $value . “n”;
}
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header (“Location:$location”);
}
else {
echo “Thank you for submitting our form. We will get back to you as soon as possible.”;
}

?>

An ASP Form Mailer

I’ve made an example form in HTML Strict 4.01 and there’s also the script itself in a separate .asp file–you can download them here.

It’s pretty simple to use: the form submits the info to the ASP file which processes the it and then redirects the user to another page. All you have to do is open up the ASP file in Notepad or any text editor and fill in values for three variables:

SMTPserver	= "xxx.xxx.xxx.xxx"		' Specify a valid SMTP mail server.
redir		= "http://www.w3.org/"		' Specify a page to redirect the user to once they have submitted the form.
recipient	= "email@address.com"		' Specify who to send the email to.Attached

Note: the Persits AspEmail component must be installed and configured on the server for the ASP example to work. AspEmail is available on most modern Windows Servers. See the Persits Web Site for more information.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured