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!!


In reply to CGI Checkboxes by michellem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.