Databases Reference
In-Depth Information
while the related self_url( ) function returns the URL of the script, together with the
query string that preserves entered form information—for example:
http://localhost/cgi-bin/popup_menu.pl?Username=saied;Host=wombat.cs.rmit.edu.au
Example 18-5 is a single script to display a drop-down list of animal names, and display
the count of the animal that the user selects.
Example 18-5. A CGI Perl script that generates a form with a drop-down list
#!/usr/bin/perl
use strict;
# Connect to the MySQL server, run the query, store the result
use DBI;
my $dbh=DBI->connect("DBI:mysql:host=localhost;database=AnimalDB",
" the_username ",
" the_password ",
{PrintError=>0, RaiseError=>1});
# Prepare and display the results in HTML format
use CGI ':all';
print header(-type=>"text/html", -charset=>'UTF-8');
my @results;
if(!param())
{
@results = @{ $dbh->selectcol_arrayref
("SELECT Name FROM Animals ORDER BY Name")};
print
start_html(-title=>"Animal Selection Page", -encoding => 'UTF-8').
h1("Select Animal").
p("Select the animal name from the list: ").
start_form(-action=>url(), -method=>'POST').
"Animal Name: ".
popup_menu(-name => "Name", -values => \@results).
br().
submit(-name=>'submit_button', -value=>'Submit query').
end_form;
}
else
{
my $Query="SELECT Count FROM Animals where Name='".param("Name")."'";
@results = @{ $dbh->selectcol_arrayref ($Query)}
or die("Problem with query $Query: ".$DBI::errstr);
print
start_html(-title=>"Animal Counts Page", -encoding => 'UTF-8').
h1("Query result").
"The count of ".
b(param("Name")).
" is: ".
@results;
}
 
Search WWH ::




Custom Search