Forms and Form Data
Annotated Example
#!/usr/bin/perl -w
use CGI;
use strict;
# Create a perlCGI object. It automatically picks up any arguments on
# the incoming URL.
my $query = new CGI;
# Create a header, and let it default arguments, it likely knows
# better then we do.
print $query->header();
# Start the body of our document, and give it a title (shows up on the
# browser bar and on printouts). All the legal fields within the HTML
# "BODY" tag are supported, and are optional. Other unsupported
# options can be specified as well, and will be correctly added. Note
# that there is nothing actually in the body tag, just HTML modifiers
# being specified.
print $query->start_html({-title=>'Form Test One...',
-author=>'Bill Kilgallon'});
print $query->hr();
# Indicate we are creating a form.
# For this example, everything defaults. We will flesh it out later.
# print $query->startform({-method=>'post'});
print $query->startform({-method=>'get'});
# Generate a text box for data entry
# Render it regardless of if we have previously entered text or not.
print $query->h1('Please enter some text');
print $query->textfield({-name=>'TextfieldOne',
-default=>'Enter your text here',
-size=>50,
-maxlength=>100});
print $query->endform();
# Now conditionally report what the user typed in previously.
# If this is not the first time into the script...
if ($query->param()) {
# Extract out whatever the contents of our field were.
my $textInput = $query->param(-name=>'TextfieldOne');
# Generate our output
print $query->hr();
print $query->p('The text you previously entered was:'), $query->br();
print $query->em({-align=>'center'}, $textInput);
print $query->hr();
}
# And finally, end the document.
print $query->end_html;
Back to Syllabus
Previous: Parameters with Perl CGI
Next: Excercise