【问题标题】:Perl CGI - Picking Radio Values for SpecificityPerl CGI - 为特异性挑选单选值
【发布时间】:2016-07-03 22:13:42
【问题描述】:

我正在做一个关于 Perl CGI 的小测验,但我完全被它困住了。我的 HTML 代码如下:

<p class="question"><br>1. The answer is Number 1 </p>
<ul class="answers">
    <input type="radio" name="q1" value="a" id="q1a"><label for="q1a">1912</label><br/>
    <input type="radio" name="q1" value="b" id="q1b"><label for="q1b">1922</label><br/>
    <input type="radio" name="q1" value="c" id="q1c"><label for="q1c">1925</label><br/>
    <input type="radio" name="q1" value="d" id="q1d"><label            for="q1d">Summer of '69</label><br/>
</ul>

CGI 程序从单选按钮中选择名称作为其参数值。我的 Perl 代码如下:

if ( param("q1") eq undef ) {
    $an1 = 0;
    $winner = 0;
print <<"BIG";
        <h1>You didn't enter the right answer</h1>
BIG
}
else {
print <<"BIG";
        <h1>You entered the right answer</h1>
BIG
}

此时,如果我选中任何一个单选框,它就会说我输入了正确的答案。

有没有什么方法可以指定它从收音机中提取的值,例如 abcd 作为参数,还是我完全做错了?

【问题讨论】:

    标签: html perl


    【解决方案1】:

    有关如何处理无线电组的详细信息,请参阅CGI documentation。 举个例子:

    my $cgi = CGI->new;
    my $value = $cgi->param('q1');
    
    if ($value eq 'a') { #correct answer
    
    }
    else { # incorrect answer
    }
    

    另外,eq 是一个字符串比较运算符,不要用它来测试undef。 Perl 对此有一个defined 函数。

    【讨论】:

      【解决方案2】:

      你应该试试这个

      $radio_value = $cgi->param('q1')
      

      在用 BIG 关闭 html 标记后,它似乎还没有结束,因为它是钢红色的!你用 print 写那行!

      【讨论】:

      • 堆栈溢出语法高亮器在 Perl 中不是很聪明。但你是对的,HEREDOC 分隔符后应该有空行。
      【解决方案3】:

      必须始终use strictuse warnings 'all'每个 Perl 程序的顶部。有了这些,您会看到消息Use of uninitialized value in string eq

      您无法使用字符串比较器eq 将值与undef 进行比较。在您的情况下,param("q1") 将是 abcd,或者如果未选择任何单选按钮,则可能是 undef。 (您通常会使用checked="checked" 选择默认选中的单选按钮之一以避免这种情况。)

      这是一个运行良好的基本 CGI 程序。

      use strict;
      use warnings 'all';
      
      use CGI::Minimal;
      use File::Spec::Functions 'abs2rel';
      
      my $self = abs2rel($0, $ENV{DOCUMENT_ROOT});
      
      my $cgi = CGI::Minimal->new;
      
      my $q1 = $cgi->param('q1') // 'none';
      
      my $message = 
              ($q1 eq 'a') ?    "<h3>You entered the right answer</h3>" :
              ($q1 ne 'none') ? "<h3>You didn't enter the right answer</h3>" :
              '';
      
      print <<END;
      Content-Type: text/html
      
      <html>
          <head>
              <title>Test Form</title>
          </head>
          <body>
      
              <form action="$self">
      
                  <p class="question"><br/>
                  1. The answer is Number 1
                  </p>
      
                  <ul class="answers">
      
                      <input type="radio" name="q1" value="none" id="q1a" checked="checked" />
                      <label for="q1a"><i>Please choose an answer</i></label>
                      <br/>
      
                      <input type="radio" name="q1" value="a" id="q1a" />
                      <label for="q1a">1912</label>
                      <br/>
      
                      <input type="radio" name="q1" value="b" id="q1b" />
                      <label for="q1b">1922</label>
                      <br/>
      
                      <input type="radio" name="q1" value="c" id="q1c" />
                      <label for="q1c">1925</label>
                      <br/>
      
                      <input type="radio" name="q1" value="d" id="q1d" />
                      <label for="q1d">Summer of '69</label>
                      <br/>
                  </ul>
      
                  <input type="submit">
      
              </form>
      
              $message
      
          </body>
      </html>
      
      END
      

      【讨论】:

        猜你喜欢
        • 2016-06-26
        • 1970-01-01
        • 1970-01-01
        • 2022-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多