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

Hi fellow monks,
I'm writing a fairly complex database editing program, and I'm stuck on checkboxes. I'm printing out a form, with the values filled in, for folks to edit. When a field is a checkbox, the data is saved to the database in the form of a comma delimited list. I've got this code snippet to get the params into the form:
sub print_form() { my %field_ref; my $field_ref = shift; #first, set the params my $q = new CGI(""); my %real_fields; while (my $key = each %$field_ref) { $real_fields{$key} = $field_ref->{$key}; my $index = index $configs{checkboxes},$key; if ($index != -1) { my @check_array = split(',',$real_fields{$key}); $q->param(-name=>"$key", -value=>"\@check_array"); } else { $q->param(-name=>"$key", -value=>"$real_fields{$key}"); } }
And then I go on to print the form...
elsif ($type =~ /check/) { my %hash = (); $type =~ s/check/checkbox/; (my $values, my $labels) = split (':',$params[0]); my @checks_list = split (';',$values); my @clabels_list = split (';',$labels); @hash{@checks_list} = @clabels_list; foreach (keys %hash) { print checkbox(-name=>"$name", -value=>$_, -label=>"$hash{$_}" +); } print br();
(@params is an array that holds the form parameters for each form element)

Any advice? The checkboxes themselves print out fine, but nothing is checked. When I check off things on the form, that data makes it into the database just fine.
Thanks!!

Replies are listed 'Best First'.
Re: CGI Checkboxes
by davorg (Chancellor) on Oct 26, 2001 at 18:51 UTC

    Nothing is checked because you don't use the -checked parameter to checkbox.

    Have you considered the checkbox_group function - it may make your life easier.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      But I've fed the data into params - it works fine for menus - the right menu item is highlighted.

        The param method doesn't seem to work for checkboxes. Here's a cut-down version of what (I think) you're doing:

        param('checkbox', 1); print checkbox(-name=>'checkbox', -value=>'checkbox', -label=>'My Checkbox');

        Which prints:

        <INPUT TYPE="checkbox" NAME="checkbox" VALUE="checkbox">My Checkbox

        Whereas code like this:

        print checkbox(-name=>'checkbox2', -value=>'checkbox2', -label=>'My Other Checkbox', -checked=>1);

        Prints what (I think) you want

        <INPUT TYPE="checkbox" NAME="checkbox2" VALUE="checkbox2" CHECKED>My O +ther Checkbox
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

      OK - I tried checkbox_group, with this code:
      } elsif ($type =~ /check/) { my %hash = (); $type =~ s/check/checkbox/; print "$english:\n"; (my $values, my $labels) = split (':',$params[0]); my @checks_list = split (';',$values); my @clabels_list = split (';',$labels); @hash{@checks_list} = @clabels_list; print $q->checkbox_group(-name=>"$name", -values=>\@checks +_list,-default=>\@check_array, -labels=>\%hash); print $q->br(); }

      Still no checked boxes. :-(

      Well, it appears that neither checkboxes nor scrolling lists (with multiple selected) will work. I'm stumped. I must be doing something wrong, but I just don't see it.

      This checkbox code doesn't work:

      foreach(@checks_list) { my $index=index $real_fields{$name},$_; if ($index != -1) { print $q->b('This should be checked!'); print $q->checkbox(-checked=>'checked', -name=>"$nam +e", -value=>"$_", -label=>"$hash{$_}"); } else { print $q->checkbox(-name=>"$name", -value=>"$_", -la +bel=>"$hash{$_}"); } }
      Nor does this:
      print $q->checkbox_group(-name=>"$name", -values=>\@checks_list,-defa +ult=>\@check_array, -labels=>\%hash);
      nor does this:
      print $q->scrolling_list(-name=>"$name", -values=>\@checks +_list, -default=>\@check_array, -labels=>\%hash, -multiple=>'true');
Re: CGI Checkboxes
by miyagawa (Chaplain) on Oct 26, 2001 at 18:51 UTC
      I'm trying to reduce the number of modules needed for this project - it's designed for people that probably don't have tha ability to install modules - and I don't want to have to add too many to the install package, if it's easy to get away with it.
First Normal Form (was Re: CGI Checkboxes)
by mandog (Curate) on Oct 26, 2001 at 20:16 UTC
    michellem writes:
    When a field is a checkbox, the data is saved to the database in the form of a comma delimited list.

    I know that this is not what you are asking for...

    If I understand you correctly, you are planning to stuff a list of values into a single column.

    This may not be a good idea. The first three normal forms are probably more important to datbase programming than use strict and use warnings are to perl programming.

    You are (apparently) violating the First Normal Form. Whole books have been written on the subject and there are many, mnay good reasons to normalize. One of them is that normalized data is a lot easier to deal with. SQL is to Perl, as Perl is to C in terms of the ratio of lines of code to results.

    Sorry, for lack of more specific examples. Hope this helps



    email: mandog

      mandog writes
      You are (apparently) violating the First Normal Form.

      Well, yes and no. I understand well the idea of normalizing databases, and use it when I design my own databases. But I'm not writing a specific application for a specific database - I'm writing a generic application to allow people to edit their own database data, using schema and form elements that they have designed. On the web, this generally flat file, not relational (although I'm working on getting this to work for relational databases).

      The standard way that checkboxes work is to allow multiple choices within one field. I'm just trying to allow people to work with this standard. If they know something about database atomicity, then they should design their databases accordingly. :-)

        I don't know if a working example would help, but here it is anyway:

        #!/perl/bin/perl -w use strict; use CGI; my $query = new CGI; my @checks_list = qw(eenie meenie minie moe); my @check_array = qw(eenie moe); my %hash = ( eenie => 'Eenie', meenie => 'Meenie', minie => 'Minie', moe => 'Moe'); print $query->header(), $query->start_html(), $query->start_form(), $query->checkbox_group( -name => 'group_name', -values => \@checks_list, -default => \@check_array, -labels => \%hash, -rows => 2, -columns => 2), $query->end_form(), $query->end_html();