来源:
http://www.cnblogs.com/itech/archive/2012/09/23/2698595.html
http://www.cnblogs.com/itech/archive/2012/10/31/2748044.html
一 此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的处理结果
1 #!/usr/bin/perl -Tw 2 # 3 # PROGRAM: scrolling_list.cgi 4 # 5 # PURPOSE: Demonstrate (1) how to create a scrolling_list form and 6 # (2) how to determine the value(s) selected by the user. 7 # 8 # Created by alvin alexander, devdaily.com. 9 # 10 11 #-----------------------------------# 12 # 1. Create a new Perl CGI object # 13 #-----------------------------------# 14 15 use CGI; 16 $query = new CGI; 17 18 19 #----------------------------------# 20 # 2. Print the doctype statement # 21 #----------------------------------# 22 23 print $query->header; 24 25 26 #----------------------------------------------------# 27 # 3. Start the HTML doc, and give the page a title # 28 #----------------------------------------------------# 29 30 print $query->start_html('My scrolling_list.cgi program'); 31 32 33 #------------------------------------------------------------# 34 # 4a. If the program is called without any params, print # 35 # the scrolling_list form. # 36 #------------------------------------------------------------# 37 38 if (!$query->param) { 39 40 print $query->startform; 41 print $query->h3('Select your favorite programming language(s):'); 42 print $query->scrolling_list(-name=>'languages', 43 -values=>[ 44 'Basic', 45 'C', 46 'C++', 47 'Cobol', 48 'DHTML', 49 'Fortran', 50 'HTML', 51 'Korn Shell (Unix)', 52 'Perl', 53 'Java', 54 'JavaScript', 55 'Python', 56 'Ruby', 57 'Tcl/Tk'], 58 -size=>8, 59 -multiple=>'true', 60 -default=>'Perl'); 61 62 # Notes: 63 # ------ 64 # "-multiple=>'true'" lets the user make multiple selections 65 # from the scrolling_list 66 # "-default" is optional 67 # "-size" lets you specify the number of visible rows in the list 68 # can also use an optional "-labels" parameter to let the user 69 # see labels you want them to see, while you use 70 # different names for each parameter in your program 71 72 print $query->br; 73 print $query->submit(-value=>'Submit your favorite language(s)'); 74 print $query->endform; 75 76 } else { 77 78 #----------------------------------------------------------# 79 # 4b. If the program is called with parameters, retrieve # 80 # the 'languages' parameter, assign it to an array # 81 # named $languages, then print the array with each # 82 # name separated by a <BR> tag. # 83 #----------------------------------------------------------# 84 85 print $query->h3('Your favorite languages are:'); 86 @languages = $query->param('languages'); 87 print "<BLOCKQUOTE> "; 88 foreach $language (@languages) { 89 print "$language<BR>"; 90 } 91 print "</BLOCKQUOTE> "; 92 93 } 94 95 #--------------------------------------------------# 96 # 5. After either case above, end the HTML page. # 97 #--------------------------------------------------# 98 print $query->end_html;
二 也可以实现为html+perlcgi
代码:(多选checkbox实例)
1 #colors.html 2 <html><head><title>favorite colors</title></head> 3 <body> 4 5 <b>Pick a Color:</b><br> 6 7 <form action="colors.cgi" method="POST"> 8 <input type="checkbox" name="red" value=1> Red<br> 9 <input type="checkbox" name="green" value=1> Green<br> 10 <input type="checkbox" name="blue" value=1> Blue<br> 11 <input type="checkbox" name="gold" value=1> Gold<br> 12 <input type="submit"> 13 </form> 14 </body> 15 </html> 16 17 #colors.cgi 18 #!/usr/bin/perl -wT 19 20 use CGI qw(:standard); 21 use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 22 23 print header; 24 print start_html; 25 26 my @colors = ("red", "green", "blue", "gold"); 27 foreach my $color (@colors) { 28 if (param($color)) { 29 print "You picked $color.<br> "; 30 } 31 } 32 33 print end_html;
其他实例radiobox
1 #radiobox.html 2 <html><head><title>Pick a Color</title></head> 3 <body> 4 <b>Pick a Color:</b><br> 5 6 <form action="radiobox.cgi" method="POST"> 7 <input type="radio" name="color" value="red"> Red<br> 8 <input type="radio" name="color" value="green"> Green<br> 9 <input type="radio" name="color" value="blue"> Blue<br> 10 <input type="radio" name="color" value="gold"> Gold<br> 11 <input type="submit"> 12 </form> 13 </body></html> 14 15 #radiobox.cgi 16 #!/usr/bin/perl -wT 17 use strict; 18 use CGI qw(:standard); 19 use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 20 21 my %colors = ( red => "#ff0000", 22 green => "#00ff00", 23 blue => "#0000ff", 24 gold => "#cccc00"); 25 26 print header; 27 my $color = param('color'); 28 29 # do some validation - be sure they picked a valid color 30 if (exists $colors{$color}) { 31 print start_html(-title=>"Results", -bgcolor=>$color); 32 print "You picked $color.<br> "; 33 } else { 34 print start_html(-title=>"Results"); 35 print "You didn't pick a color! (You picked '$color')"; 36 } 37 print end_html;
三 cgi实例2
1 #!/usr/bin/perl 2 use strict; 3 use warnings; 4 use CGI; 5 use CGI::Carp qw(fatalsToBrowser); 6 7 sub output_top($); 8 sub output_end($); 9 sub display_results($); 10 sub output_form($); 11 12 my $q = new CGI; 13 14 print $q->header(); 15 16 # Output stylesheet, heading etc 17 output_top($q); 18 19 if ($q->param()) { 20 # Parameters are defined, therefore the form has been submitted 21 display_results($q); 22 } else { 23 # We're here for the first time, display the form 24 output_form($q); 25 } 26 27 # Output footer and end html 28 output_end($q); 29 30 exit 0; 31 32 # Outputs the start html tag, stylesheet and heading 33 sub output_top($) { 34 my ($q) = @_; 35 print $q->start_html( 36 -title => 'A Questionaire', 37 -bgcolor => 'white', 38 } 39 40 # Outputs a footer line and end html tags 41 sub output_end($) { 42 my ($q) = @_; 43 print $q->div("My Web Form"); 44 print $q->end_html; 45 } 46 47 # Displays the results of the form 48 sub display_results($) { 49 my ($q) = @_; 50 51 my $username = $q->param('user_name'); 52 print $username; 53 print $q->br; 54 55 # Outputs a web form 56 sub output_form($) { 57 my ($q) = @_; 58 print $q->start_form( 59 -name => 'main', 60 -method => 'POST', 61 ); 62 63 print $q->start_table; 64 print $q->Tr( 65 $q->td('Name:'), 66 $q->td( 67 $q->textfield(-name => "user_name", -size => 50) 68 ) 69 ); 70 71 print $q->Tr( 72 $q->td($q->submit(-value => 'Submit')), 73 $q->td(' ') 74 ); 75 print $q->end_table; 76 print $q->end_form; 77 }
更多实例
http://www.cgi101.com/book/ch5/text.html
http://www.comp.leeds.ac.uk/Perl/Cgi/forms.html
四 cgi实例3
1 #!/usr/local/bin/perl 2 use CGI ':standard'; 3 4 print header; 5 print start_html("Example CGI.pm Form"); 6 print "<h1> Example CGI.pm Form</h1> "; 7 print_prompt(); 8 do_work(); 9 print_tail(); 10 print end_html; 11 12 sub print_prompt { 13 print start_form; 14 print "<em>What's your name?</em><br>"; 15 print textfield('name'); 16 print checkbox('Not my real name'); 17 18 print "<p><em>Where can you find English Sparrows?</em><br>"; 19 print checkbox_group( 20 -name=>'Sparrow locations', 21 -values=>[England,France,Spain,Asia,Hoboken], 22 -linebreak=>'yes', 23 -defaults=>[England,Asia]); 24 25 print "<p><em>How far can they fly?</em><br>", 26 radio_group( 27 -name=>'how far', 28 -values=>['10 ft','1 mile','10 miles','real far'], 29 -default=>'1 mile'); 30 31 print "<p><em>What's your favorite color?</em> "; 32 print popup_menu(-name=>'Color', 33 -values=>['black','brown','red','yellow'], 34 -default=>'red'); 35 36 print hidden('Reference','Monty Python and the Holy Grail'); 37 38 print "<p><em>What have you got there?</em><br>"; 39 print scrolling_list( 40 -name=>'possessions', 41 -values=>['A Coconut','A Grail','An Icon', 42 'A Sword','A Ticket'], 43 -size=>5, 44 -multiple=>'true'); 45 46 print "<p><em>Any parting comments?</em><br>"; 47 print textarea(-name=>'Comments', 48 -rows=>10, 49 -columns=>50); 50 51 print "<p>",reset; 52 print submit('Action','Shout'); 53 print submit('Action','Scream'); 54 print end_form; 55 print "<hr> "; 56 } 57 58 sub do_work { 59 60 print "<h2>Here are the current settings in this form</h2>"; 61 62 for my $key (param) { 63 print "<strong>$key</strong> -> "; 64 my @values = param($key); 65 print join(", ",@values),"<br> "; 66 } 67 } 68 69 sub print_tail { 70 print <<END; 71 <hr> 72 <address>Lincoln D. Stein</address><br> 73 <a href="/">Home Page</a> 74 END 75 }
具体的更多的form(checkbox,check_group,radio_group,popup_menu,hidden,scrolling_list,textarea.......), 在manpage search: http://search.cpan.org/~markstos/CGI.pm-3.60/lib/CGI.pm