Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I got a form with about 100 checkboxes. They all have the same name ('desig'). In what way can I read the values that were checked?

Thanks,
Ralph.

Replies are listed 'Best First'.
Re: Reading Checkboxes
by Fastolfe (Vicar) on Nov 05, 2001 at 02:57 UTC
    Assuming you're using CGI.pm, here's a small excerpt from its documentation:
    When the form is processed, all checked boxes will be returned as a list under the parameter name 'group_name'. The values of the "on" checkboxes can be retrieved with: @turned_on = $query->param('group_name');
    Substitute 'group_name' with 'desig' and you've got an array of each checked checkbox's value.
Re: Reading Checkboxes
by greywolf (Priest) on Nov 05, 2001 at 11:26 UTC
    HMTL tip coming up:
    All the checkboxes in the form must have different names. If they have the same name you will only get one value = 'on', even if all boxes are checked.

    Update:
    I stand corrected. The broken solution presented by Fastolfe is what I have had to use at work. I will have to try ColtsFoots solution next time around.

    mr greywolf
      This does not seem to be the case, the following code
      #!/usr/bin/perl -w use strict; use CGI; my $cgi = new CGI; print $cgi->header(); print $cgi->start_html(); my $submit = $cgi->param('submit'); if ($submit) { my @on = $cgi->param('desig'); foreach my $item (@on) { print qq($item<BR>); } exit; } print $cgi->startform(-method=>'post'); print $cgi->checkbox_group(-name=>'desig', -values=>['a', 'b', 'c', 'd', 'e', 'f', 'g']); print $cgi->submit(-name=>'submit', -value=>'Submit'); print $cgi->endform(); print $cgi->end_html();
      yields
      a c e g
      when every other box is checked
      Only if you're using a broken/proprietary CGI parameter parsing routine that does something like this:
      foreach my $pair (split /&/, $ENV{QUERY_STRING}) { my ($key, $value) = split(/=/, $pair); $parameters{$key} = $value; }
      The correct solution is to defer processing like this to CGI.pm, which will correctly handle multiple checkboxes with the same name.