简体   繁体   English

Perl CGI-为特定性选择无线电值

[英]Perl CGI - Picking Radio Values for Specificity

I've got a little Perl CGI quiz that I am working on, but I'm completely stuck on it. 我正在做一些Perl CGI测验,但是我完全坚持了下来。 My HTML code is as follows: 我的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> 

The CGI program is picking the name from the radio button for its parameter value. CGI程序正在从单选按钮中选择名称作为其参数值。 My Perl code is as follows: 我的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
}

At this point, it will say I entered the right answer if I check any of the radio boxes. 此时,如果我选中任何一个单选框,它将表示我输入了正确的答案。

Is there some way I can specify which value it plucks from radio, like a or b or c or d for the parameter, or am I doing it wrong altogether? 有什么方法可以指定从无线电中提取的值,例如参数的abcd ,还是我做错了?

Please, refer to CGI documentation for details on how to process radio group. 请有关详细信息,请参阅CGI文档 To give you an example: 举个例子:

my $cgi = CGI->new;
my $value = $cgi->param('q1');

if ($value eq 'a') { #correct answer

}
else { # incorrect answer
}

Also, eq is a string comparison operator, don't use it to test for undef . 同样, eq是一个字符串比较运算符,不要用它来测试undef Perl has a defined function for this. Perl为此具有defined功能。

You must always use strict and use warnings 'all' at the top of every Perl program. 必须始终每个 Perl程序的顶部始终 use strict use warnings 'all'use warnings 'all' With those in place you would have seen the message Use of uninitialized value in string eq 有了这些,您将看到消息Use of uninitialized value in string eq

You can't compare a value to undef using the string comparator eq . 您不能使用字符串比较器eq将值与undef进行比较。 In your case, param("q1") will be a , b , c or d , or perhaps undef if none of the radio buttons are selected. 在您的情况下, param("q1")将为abcd ,或者如果未选择任何单选按钮,则可能为undef (You would generally make one of the radio buttons selected by default to avoid this, using checked="checked" .) (为避免这种情况,您通常会选择默认选中其中一个单选按钮,方法是使用checked="checked" 。)

Here's a basic CGI program that works fine. 这是一个运行良好的基本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

you should try this 你应该试试这个

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

also after closing html tag with BIG it seems that it is not ending because its steel red!you write that lines with print! 在用BIG关闭html标记后,似乎也没有结束,因为它的钢红色!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM