Forms Sample #1

Textfield

Text area

Radio Buttons (choose one)

Checkboxes (choose many)

Selection drop-down (choose one)

Selection list (choose one or chose many)


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";
print "<TITLE>Form Echo</TITLE>";
print "<H1>Form Echo</H1>";
print "<H2>Here is what you typed:</H2>\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. We'll also print out
# each pair as it is created.
#
@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;

    print "<B>$name</B> = $value<BR>\n";

    $FORM{$name} = $value;
}

# 
# As an extra bonus, here's how you get environment variables from
# someone submitting a script.
#
print "<HR>";
print "<H2>Environment Variables</H2>\n";

foreach $item (keys %ENV)
{
    print "<B>$item</B> = " . $ENV{"$item"} . "<BR>\n";
};

#
# This script does something unusual: it prints out the name/value
# pairs as they're being created. That's great for an echo demo, but
# it's not really what you normally do.
#
# 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.
#

print "<HR>";
print "<H2>A Real Example</H2>\n";
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.";

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