in reply to Re: CGI Checkboxes
in thread CGI Checkboxes

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

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

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