设为首页 收藏本站
查看: 673|回复: 0

[经验分享] perl-cgi-form

[复制链接]

尚未签到

发表于 2015-12-25 15:31:23 | 显示全部楼层 |阅读模式
  

  一  此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

  完!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156296-1-1.html 上篇帖子: Perl包和模块 下篇帖子: perl函数原型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表