Forms Sample #3

This script will take the information you've entered and mail it to you. It's important that you type in a real email address, and preferably your own.

Email Address:


I have: A fish A bird A cat A dog

I prefer:


Here is the HTML that created that form:
<FORM METHOD="POST" ACTION="formex3.cgi">

Email Address: <INPUT SIZE=45 NAME="email" VALUE="">
Name: <INPUT SIZE=25 NAME="name" VALUE="">
I have:   <INPUT type="checkbox" NAME="haveafish" CHECKED>A fish 
          <INPUT type="checkbox" NAME="haveabird">A bird 
          <INPUT type="checkbox" NAME="haveacat" CHECKED>A cat 
          <INPUT type="checkbox" NAME="haveadog">A dog 
I prefer: <SELECT NAME="prefer">
<INPUT TYPE=submit VALUE="Go for it!">
<INPUT TYPE=reset VALUE="No, no, start over">

The Script

Here is the script that will be run:
# This script was originally written by Michael Toy, and modified
# by Hagan Heller in March, 1995.
# The first line should be the path to "perl", which is the
# language that this script was written in. If you don't have
# perl, you can't run this. Also, MAKE SURE that the execute 
# permission is turned on for this file.

# If you're going to send mail, first you have to set up a mail program.
# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';

# The information "returned" from this script must have a
# type, so that Netscape will know what to do with it. The
# type text/html tells it to display this as a document
# Anything that's print-ed after this shows up in the document.
print "Content-type: text/html\n\n";

# This reads in the information sent when the user pressed Submit
if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer = $ENV{'QUERY_STRING'}; }
else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }

# Now, using a little loop, we'll split up the data into name/value
# pairs, which makes them easier to work with. 
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
    ($name, $value) = split(/=/, $pair);

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;

# Once the name/value pairs have been created, you can work with
# them by referring to the variable names you set up in the 
# original HTML, using $FORM{"varname"}.

# First, we make sure that they actually gave an email address
if ($FORM{"email"} eq "") {
    print "<TITLE>Entry Not Accepted</TITLE>";
    print "<H1>Entry Not Accepted:</H1><P>\n";
    print "<H1>Email: was left blank</H1>\n";
    print "You have left the email field blank.  Please fill in your ";
    print "correct email address and re-submit the form, so that it ";
    print "can send you email.";

# You need the exit command, otherwise it will keep going on
# in the script and do more stuff.

# Send mail to
if ($recipient eq "") { $recipient = $FORM{'email'}; }

# Open a mail file and begin writing to it
format MAIL =
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# When you're printing, put in a newline \n to make a return
# Be careful using quotes inside of quotes. It's ok to use 'this "is" text'
# of "this 'is' text", but dont mix things up.
# Also, don't forget those semi-colons!

open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL "From: $FORM{'email'}\n";
print MAIL "Subject: Form test from $FORM{'name'})\n\n";

print MAIL "Dear $FORM{name},\n\n";
print MAIL "Thank you for visiting us from $ENV{HTTP_REFERER}.\n";
print MAIL "We are so happy to hear that you prefer $FORM{prefer}.\n";
print MAIL "We have some valuable products to offer you! Please contact\n";
print MAIL "us!\n ";

close (MAIL);

# Now that the mail has been sent, put up something in the document
# to let the user know it went well
print "<H2>Mail Has been sent.</H2>\n";
print " Your email has been sent to $FORM{'email'}.";

Copyright © 1995 Hagan Heller. The information on these pages may be freely copied, so long as they are not sold and this copyright information is included.