#!/usr/bin/perl
###############################################################################
#formTest: Test the HTML Form Elements
# print form element to HTML page,
###############################################################################
%formData = &getFormData(); # get form data into hash
&printData(); # call printData to print
###############################################################################
# printData: print form data elements into an HTML form
###############################################################################
sub printData {
print "Content-type: text/html \n";
print "\n";
print "
Print Form Elements
";
print "";
print "REQUEST_METHOD: | $ENV{'REQUEST_METHOD'} \n";
print " |
QUERY_STRING: | $ENV{'QUERY_STRING'} \n";
print " |
STDIN data input: | $buffer \n";
print " |
CONTENT_LENGTH: | $ENV{'CONTENT_LENGTH'} \n";
print " |
";
print "
Your Form Elements and Values are:
";
while(($key,$value) = each(%formData)) {
print "$key | ";
print "$value ";
}
print "
|
";
}
#############################################################################
# getFormData will decode the URLencoded data from a form.
# It will return the decoded name/value pairs as a hash.
# By default will determine how to get the data (ie GET or POST)
# by examining the REQUEST_METHOD environment variable.
# Call syntax: %formData = getFormData();
#############################################################################
sub getFormData {
%form; #define an empty hash
if ($method eq "") { #if method type is blank
$method = $ENV{'REQUEST_METHOD'}; #set method to REQUEST_METHOD
}
if ($method =~ /post/i) { #if method is POST:
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); #read STDIN info buffer
} # length=CONTENT_LENGTH
else { #if method is GET:
$buffer = $ENV{'QUERY_STRING'}; #set buffer=QUERY_STRING
}
@nameValue = split(/&/, $buffer); #split up the input on &
foreach $pair (@nameValue) {
($name, $value) = split(/=/, $pair); #split up the name/value pairs
$name =~ tr/+/ /; #replace all + with a space
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /; #replace all + with a space
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#decode any escaped hex char
$value =~ s/\cM//eg; #delete all ctrl-M char
if ($form{$name}) { #if field already has a value
$form{$name} .= ", $value"; # then append to existing one
}
else { #otherwise
$form{$name} = $value; # load value for first time
}
}
$buffer = '' if ($method =~ /get/i); #if get method, clean up buffer
return %form;
}