Using Hidden Fields

Hidden fields are best generated by the same CGI script that processes the initial form - that is, the one that contains the data to be stored. Then you'll create a second script to process the final form - the one that contains both the new data and the stored data.

The bottom line is - the script that processes the data to be stored should generate the hidden fields in the next form.

Hidden fields are useful but transitory. Once your visitor leaves your site or even jumps to a page that's outside the realm of the interconnected scripts that store and generate the hidden fields, the connection between the visitor and the information you're currently collecting is lost.

Parse the Form using the same procedure as those used in Week 6 Perl CGI's section.

********** hidden1.pl **********

#!/usr/local/bin/perl

read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $input);

foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);
     $value =~ tr/+/ /;
     $value =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $formdata{$name} = $value;
}

print "Content-type:text/html\n\n";

print "<HTML>\n";
print "<HEAD><TITLE>Using Hidden Fields</TITLE></HEAD>\n\n";

print "<BODY bgColor=\"white\">\n\n";

print "Thank you, $formdata{'name'}, for entering your personal data.<BR>\n";

print "Now you can choose which items you'd like to purchase.<P>\n";

print "<FORM METHOD=\"POST\" ACTION=\"hidden2.pl\">\n";
print "Item <INPUT TYPE=\"text\" NAME=\"item\" VALUE=\"12345\">\n";

# %formdata is the name of the Associative Array the contains the
# NAME-VALUE pairs from the Form, ie,
# %formdata equals ("name", "John Smith", "state", "California) =>
# (NAME1, VALUE1, NAME2, VALUE2) in this case
#
# key is equivalent to NAME while $formdata($key) is equivalent to VALUE
# keys %formdata equals (name, state)
# $formdata(name) is "John Smith" & $formdata(state) is "California"
#
# The foreach loop creates the hidden fields for each key-value pair
# that was collected from the Form

foreach $key (keys %formdata) {
     print "<INPUT TYPE=\"hidden\" NAME=\"$key\" VALUE=\"$formdata{$key}\">\n";

}

print "<INPUT TYPE=\"submit\" VALUE=\"Send order\">\n";
print "</FORM>\n";

print "</BODY>\n";
print "</HTML>\n";

********** End of hidden1.pl **********

********** hidden2.pl **********

#!/usr/local/bin/perl

read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $input);

foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);
     $value =~ tr/+/ /;
     $value =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $formdata{$name} = $value;
}

print "Content-type:text/html\n\n";

print "<HTML>\n";
print "<HEAD><TITLE>Using Hidden Fields</TITLE></HEAD>\n\n";

print "<BODY bgColor=\"white\">\n";

print "The item ordered by $formdata{'name'} from $formdata{'state'} is\n";

print "<P>$formdata{'item'}\n";

print "<P>Thank you for your order. It's on its way.\n\n";

print "</BODY>\n";
print "</HTML>";

********** End of hidden2.pl **********