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


 
 #!/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('&nbsp;')
     );
     print $q->end_table;
     print $q->end_form;
 }

 

更多实例
http://www.cgi101.com/book/ch5/text.html 
http://www.comp.leeds.ac.uk/Perl/Cgi/forms.html 
 

完! 

posted @ 2012-09-23 00:08  iTech  阅读(1715)  评论(0编辑  收藏  举报