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

I am a real novice (3 hours experience). However, ... I can read and use all the simple forms that return one value but can't handle Checkbox forms when the user makes more than one choice. I think that the browser sends the information as 'choice=box1' then 'choice=box2' if NAME=choice and VALUE=box1 or box2 in the HTML code. my $choice= $q->param( "choice"); gives me only the first VALUE as far as I can tell. How do I get all the values? Thanks

Replies are listed 'Best First'.
(jeffa) Re: Checkbox forms
by jeffa (Bishop) on Jun 15, 2002 at 04:08 UTC
    Welcome to Perl. :)

    All you need to do is use an array instead of a scalar:

    my @choice= $q->param("choice");
    Then you can loop through it later:
    foreach my $choice (@choice) { # do something with this $choice; }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Checkbox forms
by meiere (Initiate) on Jun 15, 2002 at 05:14 UTC
    Thanks. I realized what an idiot I was about 5 minutes after I asked. This is a great group!! Forrest Meiere (retired physics professor)
Re: Checkbox forms
by perleager (Pilgrim) on Jun 15, 2002 at 05:12 UTC
    Hello:

    I just experienced the same problem as you.

    I fixed it by changing the parse form code.
    sub parse_form { $query=$ENV{'QUERY_STRING'}; if ($query) { @pairs=split(/&/,$query); } else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } foreach $pair (@pairs) { $something_in=1; ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; + } else { $INPUT{$name} = $value; } } }



    Now we want to put the checkbox list into a array.
    @users = split(/,/,$INPUT{'users'});

    To print out all the values for the checkboxes that are checked,

    foreach $userss (@users) { print <<EOF; $userss<br> EOF }


    Hope this helps,
    Anthony
      This is not the best way to address this problem of CGI parameter parsing. Your routine will fail for any form where the form checkbox values may contain commas - This is because when you go to split the input into an array, you will end up splitting the passed checkbox value into two separate array elements. This is another reason why you should not hand-roll your own CGI parameter parsers (unless you really know what you are doing and have a justifiable reason to do so -- Personally, I cannot think of any such reasons, but am willing to leave the door open on this on :-)

      Furthermore, the context of the question was to do with the usage of CGI.pm, by far the preferred manner by which to parse CGI parameters - For further details on why this is the case, please see use CGI or die;