Forms Sample #2

The script for this form won't let you post anything if you haven't given a Name. It also replies with different information, depending on whether or not you prefer cats.

Name:

I have: A fish A bird A cat A dog

I prefer:


The HTML

Here is the HTML that created that form:

<FORM METHOD="POST" ACTION="formex2.cgi">

Name: <INPUT SIZE=25 NAME="name" VALUE="">
<P>
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 
<P> 
I prefer: <SELECT NAME="prefer">
	  <OPTION>Fish
	  <OPTION>Birds
	  <OPTION SELECTED>Cats
	  <OPTION>Dogs
	  </SELECT>
<P>
<CENTER>
<INPUT TYPE=submit VALUE="Go for it!">
<INPUT TYPE=reset VALUE="No, no, start over">
</CENTER>

The Script

Here is the script that will be run:
#!/usr/sbin/perl
#
# 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.
#

#
# 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 a name
#
if ($FORM{"name"} eq "") {
    print "<TITLE>Entry Not Accepted</TITLE>";
    print "<H1>Entry Not Accepted:</H1><P>\n";
    print "<H1>Name: was left blank</H1>\n";
    print "You have left your name blank.  Please fill in your ";
    print "name and re-submit the form.";

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

if ($FORM{"prefer"} eq "Cats") {
    print "<TITLE>Form Example #2: Cats</TITLE>";
    print "<H1>Example #2: Cats</H1>";
    print "Dear <B>$FORM{name}</B>, <P>";
    print "Thank you for visiting us from <B>$ENV{HTTP_REFERER}</B>. ";
    print "We are so happy to hear that you prefer <B>$FORM{prefer}</B>. ";
    print "We have some valuable products to offer you: kitty litter, ";
    print "organic cat food, cat toys, and delicious kitty treats. Yum! ";
    print "<P>";
    print "Press <A HREF=$ENV{HTTP_REFERER}>here</A> to return.";
    exit(0);
}


print "<TITLE>Form Example #2</TITLE>";
print "<H1>Example #2</H1>";
print "Dear <B>$FORM{name}</B>, <P>";
print "Thank you for visiting us from <B>$ENV{HTTP_REFERER}</B>. ";
print "We are so happy to hear that you don't like cats. ";
print "We have a new program, where you can exchange your ";
print "$FORM{prefer} for a cat for free. Call now!";
print "<P>";
print "Press <A HREF=$ENV{HTTP_REFERER}>here</A> to return.";

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.
Make your own free website on Tripod.com