Processing User Input
User interaction with a web application is typically a two-way affair; the system displays content to the user, who provides input, such as through a web form, that the system uses in further processing. For example, an online store application displays items for sale; the user chooses items and enters purchase information, and the system displays a receipt.
For our pet roll call example, we could allow the user to enter the name of an animal. The system could then process this information and return the number of animals of that type. This requires two web pages. First, a form is displayed to accept user input. When this form is submitted, a second page is displayed with the query result.
You can generate an HTML form that passes the submitted values to the process_form.pl file using the HTTP POST method by writing:
print
start_form(-action=>"process_form.pl", -method=>'POST').
...form content....
end_form;
Example 18-4 generates a web form with a simple text input field.
Example 18-4. A CGI Perl script that generates a simple form
#!/usr/bin/perl use strict; use CGI ':all'; print header(-type=>"text/html", -charset=>'UTF-8'). start_html(-title=>"Search Page", -encoding => 'UTF-8'); if(param()) { print "<br />The string you entered was: '".param('query')."'"; print "<br />Dumping all the submitted data..."; foreach my $Name (param()) { print "<br /><b>$Name: $Name); } } else { #start_form can take -action, -method... print start_form. "Query String: ". textfield(-name=>'query', ...
Get Learning MySQL now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.