Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518518
  • 博文数量: 126
  • 博客积分: 851
  • 博客等级: 准尉
  • 技术积分: 1287
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-06 11:21
个人简介

个人最新博客地址http://www.skylway.com/

文章分类

全部博文(126)

文章存档

2016年(2)

2014年(60)

2013年(35)

2012年(29)

分类: Python/Ruby

2013-12-24 22:33:19

原文地址:perl CGI之表单 作者:

要用CGI实现一个简单的东西,总结一下perl CGI。

  • 调入CGI module
    在脚本开始使用use CGI;

  • CGI框架

    点击(此处)折叠或打开

    1. my $query->CGI->new();               #生成CGI对象
    2. print $query->header;                #创建header
    3. print $query->start_html();          #开始html
    4.    ...
    5.    ...
    6.    ...
    7.    print $query->startform();        #开始表单
    8.      ...
    9.      ...
    10.      ...
    11.    print $query->endform();          #结束表单
    12. print $query->end_html();            #结束html
    在$query->header(),$query->start_html,$query->startform中有多重参数选项,暂且搁置一边,使用默认。

  • 一些函数
    • h1(),h2...函数:生成标题,如$query->h1("level 1 header")将字符串设置为一级标题格式。
    • textfield():用法为$query->textfield("var"),生成单行文本输入框。
      其完整参数为:

      点击(此处)折叠或打开

      1. print textfield(-name=>'field_name',
      2.      -default=>'starting value',        #未输入情况下的默认值
      3.      -size=>50,                         #规定文本框的字符宽度
      4.      -maxlength=>80);                   #规定最多输入多少个字符
    • checkbox():创建一个布尔值复选框。完整参数:

      点击(此处)折叠或打开

      1. checkbox(-name=>'checkbox_name',   #给复选框命名
      2.   -checked=>1,                     #值为1表示默认情况就是选中的
      3.   -value=>'ON',                    #复选框选中情况下的值,未提供选项时默认为"ON"
      4.   -label=>'CLICK ME');             #显示在复选框后出现的字段,如下图
    • checkbox_group():创建一个复选框组,各个选项之间不排斥,完整参数:

      点击(此处)折叠或打开

      1. print checkbox_group(-name=>'group_name',     #复选框组的名字
      2.    -values=>['eenie','meenie','minie','moe'], #各个复选框选中时候的值
      3.    -default=>['eenie','moe'],                 #默认选中的值
      4.    -linebreak=>'true', #为0表示各个选项横排,非0则竖排对其
      5.    -disabled => ['moe'],                      #无法选中显示灰色
      6.    -labels=>\%labels,  #紧跟在复选框后面显示出来的标签字段
      7.    -attributes=>\%attributes);
    • radio_group():单选按钮,如设置性别选项时即能用到,格式:

      点击(此处)折叠或打开

      1. radio_group( -name=>'group_name',
      2.    -values=>['val1','val2','val3', ...],
      3.    -default=>'val2',
      4.    -linebreak=>1,
      5.    -labels=>\%labels
      6.  );
    • popup_menu():实现下拉菜单:

      点击(此处)折叠或打开

      1. print popup_menu(-name=>'menu_name',
      2.   -values=>['eenie','meenie','minie'],
      3.   -default=>['meenie','minie'],
      4.   -labels=>\%labels,
      5.   -attributes=>\%attributes);
    • submit(),创建提交按钮,

      点击(此处)折叠或打开

      1. print submit(-name=>'button_name',#按钮名称
      2.   -value=>'value');               #提交的值????
    • reset(),创建reset按钮,作用是将各表单项恢复到上次调用cgi脚本时输入的参数,而不是恢复到默认选项。
    • default(),恢复到默认选项。



  • 将以上各函数实现代码如下,如果是windows需要开iis。

  • 点击(此处)折叠或打开

    1. #!/usr/bin/perl

    2. use warnings;
    3. use strict;
    4. use CGI;

    5. my $query = CGI->new();
    6. print $query->header;
    7. print $query->start_html("The Object_Oriented CGI and Forms");
    8. print $query->h2("Example using Forms with Pop-up Menus");

    9. my %labels= {'eenie' => 'label_eenie',
    10.      'meenie' => 'label_meenie',
    11.      'ninie' => 'label_ninie',
    12.      'moe' => 'label_moe',
    13. };
    14. print $query->startform;

    15. print "This is a textfield:";
    16. print "
      ";
    17. print $query->textfield(-name=>'field_name',
    18.      -default=>'starting value',
    19.      -size=>50,
    20.      -maxlength=>80);
    21. print "
      ";
    22. print "
      ";
    23. print "This is a checkbox:";
    24. print "
      ";
    25. print $query->checkbox(-name=>'checkbox_name',
    26.      -checked=>1,
    27.      -value=>'ON',
    28.      -label=>'CLICK ME');
    29. print "
      ";
    30. print "
      ";
    31. print "This is a checkbox_group";
    32. print "
      ";
    33. print $query->checkbox_group(-name=>'group_name',
    34.      -values=>['eenie','meenie','minie','moe'],
    35.      -default=>['eenie','moe'],
    36.      -linebreak=>'true',
    37.      -disabled => ['moe'],
    38. #-labels=>\%labels,
    39. #-attributes=>\%attributes
    40. );
    41. print $query->submit();
    42. print $query->defaults(-name=>'clear all');
    43. print $query->endform;
    44. print $query->end_html;
    还有其他,后面再说了。
阅读(3257) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~