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

Fellow Monasterians,

I like HTML::FillInForm, but I've discovered a problem populating multiple checkboxes. There was an example given here, but because I'm validating and doing stuff with all the values, I really can't use %fdat = $q->Vars; (besides, I don't know if I'm really doing anything differently with the hash anyway). Here's what I've tried

HTML: <input type="checkbox" name="typeofinfo" value="1" /> Program<br /> <input type="checkbox" name="typeofinfo" value="2" /> Abstracts<br /> <input type="checkbox" name="typeofinfo" value="3" /> Exhibits<br /> <input type="checkbox" name="typeofinfo" value="4" /> Registration<br +/> PERL: $fillinformvalues = { typeofinfo => $query->param('typeofinfo') } my $form = new HTML::FillInForm; my $page = $form->fill( scalarref => \$html, fdat => $fillinformvalues ); print $page;

It only checks the first box that was checked. I used to do it with HTML::Template in a more convoluted way, but it worked.

USING H::T <input type="checkbox" name="typeofinfo" value="1" <tmpl_if type1>chec +ked </tmpl_if>/> Program<br /> <input type="checkbox" name="typeofinfo" value="2" <tmpl_if type2>chec +ked </tmpl_if>/> Abstracts<br /> PERL: { no strict 'subs'; for my $i (0 .. $#$typeofinfo) { $template -> param( type.$typeofinfo->[$i] => 1 ) } } print $template->output;

Is there a way to make my code work with multiple checkboxes? Thanks!


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Can HTML::FillInForm handle multiple checkboxes?
by borisz (Canon) on Aug 05, 2005 at 15:14 UTC
    Your error is, that you call $query->param('typeofinfo') in scalar context. If you want a list ask for a list ;-).
    This example check 1 and 4.
    use HTML::FillInForm; use HTML::Template; use CGI; my $query = CGI->new; my $html = <<__TEMPLATE__; <input type="checkbox" name="typeofinfo" value="1" /> Program<br /> <input type="checkbox" name="typeofinfo" value="2" /> Abstracts<br /> <input type="checkbox" name="typeofinfo" value="3" /> Exhibits<br /> <input type="checkbox" name="typeofinfo" value="4" /> Registration<br +/> __TEMPLATE__ my $fillinformvalues = { typeofinfo => [ 1, 4 ] }; my $form = new HTML::FillInForm; my $page = $form->fill( scalarref => \$html, fdat => $fillinformvalues ); print $page;
    Boris
      The fix is:
      $fillinformvalues = { typeofinfo => [ $query->param('typeofinfo') ] }

        Perfect. What a couple of brackets won't do for ya!

        I knew that the CGI was returning a list, I just didn't know how to get it into my $fillinformvalues hash. Good stuff. Thanks!


        —Brad
        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      Your error is, that you call $query->param('typeofinfo') in scalar context.

      nope, in the original post it's called in list context which is causing the error.
      i think that's what you meant.

Re: Can HTML::FillInForm handle multiple checkboxes?
by trammell (Priest) on Aug 05, 2005 at 16:17 UTC
    Do you have warnings turned on? This code:
    $fillinformvalues = { typeofinfo => $query->param('typeofinfo') };
    should complain about constructing a hash with an odd number of elements, when the query object has an even number of entries for that parameter. That warning might have been a clue.