在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一 此cgi既是提交前的form,也被用来处理form的提交 来自:http://www.devdaily.com/perl/perl-cgi-example-scrolling-list-html-form 代码: (多选listbox-Multiple-choice SELECTs实例)
#!/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
#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.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 #!/usr/bin/perl
use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); sub output_top($); sub output_end($); sub display_results($); sub output_form($); my $q = new CGI; print $q->header(); # Output stylesheet, heading etc output_top($q); if ($q->param()) { # Parameters are defined, therefore the form has been submitted display_results($q); } else { # We're here for the first time, display the form output_form($q); } # Output footer and end html output_end($q); exit 0; # Outputs the start html tag, stylesheet and heading sub output_top($) { my ($q) = @_; print $q->start_html( -title => 'A Questionaire', -bgcolor => 'white', } # Outputs a footer line and end html tags sub output_end($) { my ($q) = @_; print $q->div("My Web Form"); print $q->end_html; } # Displays the results of the form sub display_results($) { my ($q) = @_; my $username = $q->param('user_name'); print $username; print $q->br; # Outputs a web form sub output_form($) { my ($q) = @_; print $q->start_form( -name => 'main', -method => 'POST', ); print $q->start_table; print $q->Tr( $q->td('Name:'), $q->td( $q->textfield(-name => "user_name", -size => 50) ) ); print $q->Tr( $q->td($q->submit(-value => 'Submit')), $q->td(' ') ); print $q->end_table; print $q->end_form; }
更多实例 完! |
请发表评论