3

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?

6
  • 2
    Where did you get that code from? Whatever book or web site you found it on, please stop reading it now. It's about fifteen years out of date and contains at least two potential problems. Commented Apr 27, 2012 at 8:47
  • @Dave I found this code here. Could you please let me know what are the problems and what should be the correct code? I would appreciate it if you put your reply as a separate answer. Commented Apr 28, 2012 at 15:24
  • 1
    Yeah. Any CGI tutorial that doesn't use CGI.pm should really be ignored. That module has been included with Perl for 15 years and nakes writing CGO programs far easier. The two problems I noted are 1/ It assumes that parameters are separated by '&' (';' is also allowed) and 2/ It breaks if a single parameter name has multiple associated values. There may well be more problems. Just use the param function from CGI.pm. Commented Apr 29, 2012 at 19:25
  • @DaveCross thanks for the suggestion! I am rewriting my code using CGI.pm now, it seems to be much neater. Commented May 1, 2012 at 17:31
  • Please stop using the dirty old CGI.pm. Use instead a modern and clean web engine such as Dancer or Mojolicious. Commented Aug 31, 2012 at 9:47

3 Answers 3

9

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";
}
Sign up to request clarification or add additional context in comments.

Comments

7
use strict; use warnings;
use CGI;

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.