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

Hello:

I have a form where a user could submit data etc.
I have 4 checkboxes
SSD [] PERS [] SS [] CHILD SUP []


Let's say a user checked the SS box and the PERS box.

Now when the user edits the form, I want the same form display and have those two items checked.

How would you do this without big if blocks?

My code:

@ss = split(/,/,$INPUT{'ss'}); foreach $ss (@ss) { if ($ss eq "ss") { print <<EOF; SS: <input type="checkbox" name="ss" value="ss" checked>&nbsp;&nbsp; EOF } elsif ($ss eq "SSD") { print <<EOF; SSD: <input type="checkbox" name="ss" value="ssd" checked>&nbsp;&nbsp +; EOF } elsif ($ss eq "PERS") { print <<EOF; PERS: <input type="checkbox" name="ss" value="pers" checked>&nbsp;&nb +sp; EOF } elsif ($ss eq "childsup") { print <<EOF; <td>CHILD SUP: <input type="checkbox" name="ss" value="childsup" +checked></td> EOF } }

Is there a module or something else that makes it easier to do this?? This is only a part of the form, I also have a field that has 9 checkboxes and that will make the if block even bigger :).
Anthony

Replies are listed 'Best First'.
(jeffa) Re: Edit page with checkboxes
by jeffa (Bishop) on Aug 04, 2002 at 19:04 UTC
    You want CGI.pm:
    use strict; use CGI qw(:standard); my @box = (qw(SSD PERS SS), 'CHILD SUP'); print header, start_html, start_form, checkbox_group( -name=>'type', -values=>\@box, ), p(submit('Go')), end_form, ; if (param('Go')) { print p(hr), 'You picked:', ul(li[param('type')]), ; } print end_html;

    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)
    
      I disagree. IT isn't really a solution to the problem either.
      You might want to use the ? : construction. like
      true ? print " selected" : print "";
      I am sure that there are monks here who can explain this better but this is the general idea.
        I think that was a fine solution to the problem. Why not use CGI.pm? And sure, there are many ways to tackle this problem - here is another that utilizes your ternary suggestion:
        use strict; my @ss = ('SSD','PERS','SS','CHILD SUP'); my %checked = map {$_ => 1} ('SSD','PERS'); foreach my $ss (@ss) { print qq|<input type="checkbox" name="ss" value="$ss"|, $checked{$ss} ? ' selected' : '', ">\n" ; }
        UPDATE:
        Actually, @checked should be populated with something like the original:
        my %checked = map {$_ => 1} split(/,/,$INPUT{'ss'})
        But that point is moot (mu?) - use CGI.pm to handle form processing.

        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: Edit page with checkboxes
by crenz (Priest) on Aug 04, 2002 at 22:30 UTC

    I think CGI.pm could do it for you, it has parameters to auto-fill in submitted values (not only checkboxes). You should read the documentation for it.

    But just focusing on your code, you could simplify it a lot. First, use a subroutine. (Note that the description for a checkbox should follow the checkbox, but I made it fit your code)

    sub checkbox { my ($name, $value, $sel) = @_; my $checked = ($value eq $sel) ? "checked" : ""; print <<EOT $value: <input type="checkbox" $checked name="$name" value="$value">&nbsp;&nbsp; EOT return ($value eq $sel); }

    This way, you can write your first two if statements like this:

    checkbox("ss", "ss", $ss); checkbox("ss", "ssd", $ss);

    Next, you could replace the first three if statements by

    my $found = 0; CHECKBOX: foreach (ss ssd pers) { if (checkbox("ss", $_, $ss)) { $found++; last CHECKBOX; } } if (not $found) { # code for those checkboxes with special treatment }

    Of course, the code could be more simplified, but I leave that as an exercise to the reader. ;-) (No, actually, you will have to do it yourself because I don't know your problem.)

Re: Edit page with checkboxes
by sauoq (Abbot) on Aug 04, 2002 at 22:32 UTC
    Well, I'm not sure I understand your question fully but given your code, something like the following would be cleaner:
    foreach $ss (split /,/,$INPUT{'ss'}) { print qq($ss: <input type="checkbox" name="ss" value=") . lc($ss) . qq(" checked>&nbsp;&nbsp;\n); }
    -sauoq
    "My two cents aren't worth a dime.";