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

I'm sorry but i just can't seem to find any proper tutorial on html::template with checkboxes and radio buttons. How do I actually retain the checked values after a validation of its form elements? Let's say a code snippet from main.pl :
my @checkboxOptions = (); for(my $i = 0; $i <= 5; $i++){ my %rowh; $checkboxStr = "<INPUT TYPE=\"CHECKBOX\" NAME=\"desc_$i\"/>My Descript +ion ($i)"; $rowh{"CHECKBOX_OPTION"} = $checkboxStr; push (@checkboxOptions, \%rowh); } $template->param("CHECKBOX_HEADER", \@checkboxOptions);
#--------------------------------- in template mytemplate.html :
<TMPL_LOOP NAME="CHECKBOX_HEADER"> <TMPL_VAR NAME="CHECKBOX_OPTION"> </TMPL_LOOP>
#----------end Is there something i could add inside the checkbox tag that would allow me to retain all the values?

Replies are listed 'Best First'.
Re: HTML::Template with dynamic checkbox
by moritz (Cardinal) on Sep 12, 2007 at 05:27 UTC
    First of all, you build HTML inside your script, not in the template - if you do that, there's no need for a templating system.

    You have to prepare the data inside your script:

    my @checkboxes = (); my $checked = 3; for (1 .. 5){ my %rowh = ( name => "desc_$_" , description => "description ($_) ", ) if ($checked == $_) { $rowh{checked} = 1; } push @checkboxes, \%rowh }

    And in the template:

    <TMPL_LOOP NAME=CHECKBOX_HEADER> <input type="checkbox" name="<TMPL_VAR NAME=name>" <TMPL_IF NAME=checked>checked="checked"</TMPL_IF>> <TMPL_VAR NAME=description</TMPL_VAR></input> </TMPL_LOOP>
      thanks moritz, you really helped me see the concept to do it!