Checkboxes with Perl CGI

拥有回忆 提交于 2019-12-02 00:50:11

问题


Sorry if my question is too simple, I am just starting out with CGI... So I have a bunch of checkboxes with the same name. Sample HTML:

<form action="/cgi-bin/checkbox.cgi" method="POST">
<input name="Loc_opt" value="Loc_1" type="checkbox">Option 1<br>
<input name="Loc_opt" value="Loc_2" type="checkbox">Option 2<br>
<input name="Loc_opt" value="Loc_3" type="checkbox">Option 3<br>
<input type="submit" value="Submit">
</form>

I need to find out which of them are checked using Perl CGI. I have the following in checkbox.cgi:

print "Content-type:text/html\r\n\r\n";
local ($buffer, @pairs, $pair, $name, $value, %FORM);
    # Read in text
    $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
    if ($ENV{'REQUEST_METHOD'} eq "POST")
    {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    }else {
    $buffer = $ENV{'QUERY_STRING'};
    }
    # Split information into name/value pairs
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs)
    {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
    }

What should I do to now print, say, the values of the selected checkboxes?


回答1:


You need to set the param() result into an array if you have multiple form elements with the same name.From CGI101:

my @colors = param('color');
foreach my $color (@colors) {
    print "You picked $color.<br>\n";
}



回答2:


use strict; use warnings;
use CGI;

my $cgi = CGI->new;
my @opt = $cgi->param('Loc_opt');



回答3:


Please read the Perl documentation for the CGI module. There are easy, built-in ways to handle all this.



来源:https://stackoverflow.com/questions/10338556/checkboxes-with-perl-cgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!