in reply to CGI Checkboxes

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."

Replies are listed 'Best First'.
Re: Re: CGI Checkboxes
by michellem (Friar) on Oct 26, 2001 at 18:52 UTC
    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."

        The param method doesn't seem to work for checkboxes.

        Oh, yes it does :-)

        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 Checkbo +x

        And it is supposed to print it -- after all, you gave it the value "1", not "checkbox"!

        Try instead:

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

        This should print so:

        <INPUT TYPE="checkbox" NAME="checkbox" VALUE="checkbox">My Checkbox <INPUT TYPE="checkbox" NAME="checkbox" VALUE="1" CHECKED>My Checkbox, +value 1

        The Sidhekin

        Thanks, Dave for the ideas - but I'm still stuck, and I'm about to lose it. Here's the code that I've generated:
        } 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; 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=>"$n +ame", -value=>"$_", -label=>"$hash{$_}"); } else { print $q->checkbox(-name=>"$name", -value=>"$_", - +label=>"$hash{$_}"); } } print $q->br(); }

        This still does not give me the right HTML code! The really weird thing is that here is an example of what I get:
        What kind of support? <b>This should be checked!</b><input type="check +box" name="form_assist" value="loan" />Loan<b>This should be checked! +</b><input type="checkbox" name="form_assist" value="grant" />Grant<b +r />
Re: Re: CGI Checkboxes
by michellem (Friar) on Oct 26, 2001 at 19:26 UTC
    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. :-(

CheckBoxes or scrolling lists
by michellem (Friar) on Oct 26, 2001 at 21:23 UTC
    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');