You want form fields to default to the last values submitted. For instance, you want a search form like Google (http://www.google.com/) where the keywords you searched for appear in the search dialog above the results.
Use CGI.pm''s HTML shortcuts to create your form, which automatically provides previous values as defaults:
print textfield("SEARCH");# previous SEARCH value is the default
Example 19-8 is a simple script for producing the list of users currently logged in.
#!/usr/bin/perl -wT
# who.cgi - run who(1) on a user and format the results nicely
$ENV{IFS}='''';
$ENV{PATH}=''/bin:/usr/bin'';
use CGI qw(:standard);
# print search form
print header( ), startl("Query Users"), h1("Search");
print start_form( ), p("Which user?",
textfield("WHO")); submit( ), end_form( );
# print results of the query if we have someone to look for
$name = param("WHO");
if ($name) {
print h1("Results");
l = '''';
# call who and build up text of response
foreach (`who`) {
next unless /^$name\s/o;# only lines matching $name
s/&/&/g; # escape HTML
s/</</g;
s/>/>/g;
l .= $_;
}
# nice message if we didn''t find anyone by that name
l = l || "$name is not logged in";
print pre(l);
}
print endl( );
The call to textfield generates HTML for a text entry field whose parameter name is WHO. After printing the form, we check whether we were called with a value for the WHO parameter. If so, we try to find lines in the output from who for that user.
The documentation for the standard CGI module; Recipe 19.4; Recipe 19.6