Flags with Perl CGI

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

<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 ones are verified using Perl CGI. I have 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 to do now to print, say, the values ​​of the selected flags?

+3
source share
3 answers

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

my @colors = param('color');
foreach my $color (@colors) {
    print "You picked $color.<br>\n";
}
+8
source
use strict; use warnings;
use CGI;

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

Read the Perl documentation for the CGI module . There are simple, built-in ways to handle all this.

+2
source