本文为大家介绍几个perl的cgi-form实例,供大家学习参考。
一、此cgi既是提交前的form,也被用来处理form的提交
来自:http://www.devdaily.com/perl/perl-cgi-example-scrolling-list-html-form
代码:(多选listbox-Multiple-choice SELECTs实例)
不带参数时即为form:http://xxxx/cgi/perl-cgi2.cgi
当点击form的submit提交时,实际上相当于:http://xxxx/cgi/perl-cgi2.cgi?languages=c&languages=html,此时为对form的处理结果
#!/usr/bin/perl -Tw
#
# PROGRAM: scrolling_list.cgi
#
# PURPOSE: Demonstrate (1) how to create a scrolling_list form and
# (2) how to determine the value(s) selected by the user.
#
# Created by alvin alexander, devdaily.com.
#
#-----------------------------------#
# 1. Create a new Perl CGI object #
#-----------------------------------#
use CGI;
$query = new CGI;
#----------------------------------#
# 2. Print the doctype statement #
#----------------------------------#
print $query->header;
#----------------------------------------------------#
# 3. Start the HTML doc, and give the page a title #
#----------------------------------------------------#
print $query->start_html('My scrolling_list.cgi program');
#------------------------------------------------------------#
# 4a. If the program is called without any params, print #
# the scrolling_list form. #
#------------------------------------------------------------#
if (!$query->param) {
print $query->startform;
print $query->h3('Select your favorite programming language(s):');
print $query->scrolling_list(-name=>'languages',
-values=>[
'Basic',
'C',
'C++',
'Cobol',
'DHTML',
'Fortran',
'HTML',
'Korn shell (Unix)',
'Perl',
'Java',
'javascript',
'python',
'Ruby',
'Tcl/Tk'],
-size=>8,
-multiple=>'true',
-default=>'Perl');
# Notes:
# ------
# "-multiple=>'true'" lets the user make multiple selections
# from the scrolling_list
# "-default" is optional
# "-size" lets you specify the number of visible rows in the list
# can also use an optional "-labels" parameter to let the user
# see labels you want them to see, while you use
# different names for each parameter in your program
print $query->br;
print $query->submit(-value=>'Submit your favorite language(s)');
print $query->endform;
} else {
#----------------------------------------------------------#
# 4b. If the program is called with parameters, retrieve #
# the 'languages' parameter, assign it to an array #
# named $languages, then print the array with each #
# name separated by a <BR> tag. #
#----------------------------------------------------------#
print $query->h3('Your favorite languages are:');
@languages = $query->param('languages');
print "<BLOCKQUOTE>n";
foreach $language (@languages) {
print "$language<BR>";
}
print "</BLOCKQUOTE>n";
}
#--------------------------------------------------#
# 5. After either case above, end the HTML page. #
#--------------------------------------------------#
print $query->end_html;
二、也可以实现为html+perlcgi
代码:(多选checkbox实例)
#colors.html
<html><head><title>favorite colors</title></head>
<body>
<b>Pick a Color:</b><br>
<form action="colors.cgi" method="POST">
<input type="checkbox" name="red" value=1> Red<br>
<input type="checkbox" name="green" value=1> Green<br>
<input type="checkbox" name="blue" value=1> Blue<br>
<input type="checkbox" name="gold" value=1> Gold<br>
<input type="submit">
</form>
</body>
</html>
#colors.cgi
#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header;
print start_html;
my @colors = ("red", "green", "blue", "gold");
foreach my $color (@colors) {
if (param($color)) {
print "You picked $color.<br>n";
}
}
print end_html;
其他实例radiobox
#radiobox.html
<html><head><title>Pick a Color</title></head>
<body>
<b>Pick a Color:</b><br>
<form action="radiobox.cgi" method="POST">
<input type="radio" name="color" value="red"> Red<br>
<input type="radio" name="color" value="green"> Green<br>
<input type="radio" name="color" value="blue"> Blue<br>
<input type="radio" name="color" value="gold"> Gold<br>
<input type="submit">
</form>
</body></html>
#radiobox.cgi
#!/usr/bin/perl -wT
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my %colors = ( red => "#ff0000",
green => "#00ff00",
blue => "#0000ff",
gold => "#cccc00");
print header;
my $color = param('color');
# do some validation - be sure they picked a valid color
if (exists $colors{$color}) {
print start_html(-title=>"Results", -bgcolor=>$color);
print "You picked $color.<br>n";
} else {
print start_html(-title=>"Results");
print "You didn't pick a color! (You picked '$color')";
}
print end_html;
三、cgi实例2
更多实例,请参考:
http://www.cgi101.com/book/ch5/text.html
http://www.comp.leeds.ac.uk/Perl/Cgi/forms.html
作者:iTech
出处:http://itech.cnblogs.com/