Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You are absolutely correct. I chose to rewrite the original using nothing but CGI.pm to demonstrate that it can be done. I do that from time to time just to keep my CGI.pm skills sharp - while i rarely use CGI.pm in production, i use it all the time here at the Monastery.

If you look carefully, you will see that checkboxes are each embedded inside their own table cell ... this makes using CGI.pm's checkbox_group() next to impossible. I'll bet a dollar or two that this was the motivation for checkbox(). This is so much easier to handle with HTML::Template, IMHO:

use strict; use warnings; use CGI qw(param header); use HTML::Template; my @entry = ( [qw(one first)], [qw(two second)], [qw(three third)], [qw(four fourth)], [qw(five fifth)], [qw(six sixth)], [qw(seven seventh)], [qw(eight eighth)], [qw(nine ninth)], [qw(ten tenth)], ); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param( result => [map {which => $entry[$_][1]}, param 'sel'], checkbox => [ { row => [map {label => $entry[$_][0], value => $_}, 0..4] }, { row => [map {label => $entry[$_][0], value => $_}, 5..9] }, ], ); print header,$tmpl->output; __DATA__ <tmpl_loop result> you checked the <tmpl_var which> box<br /> </tmpl_loop> <form method="POST"> <table> <tmpl_loop checkbox> <tr> <tmpl_loop row> <td> <tmpl_var label> <input value="<tmpl_var value>" type="checkbox" name="sel"/> </td> </tmpl_loop> </tr> </tmpl_loop> </table> <input type="submit" name="go" value="showme" /> </form>
But now we lost form stickiness. Quite some time ago, samtregar posted this node that suggests combining CGI.pm's HTML form element generating methods with HTML::Template. We can simplify the template code even further by allowing CGI.pm's td() method to handle that 'loop'. Here goes:
use strict; use warnings; use CGI qw(param header checkbox td); use HTML::Template; my @entry = ( [qw(one first)], [qw(two second)], [qw(three third)], [qw(four fourth)], [qw(five fifth)], [qw(six sixth)], [qw(seven seventh)], [qw(eight eighth)], [qw(nine ninth)], [qw(ten tenth)], ); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param( result => [map {which => $entry[$_][1]}, param 'sel'], checkbox => [ { row => td[map checkbox('sel',undef,$_,$entry[$_][0]), 0..4] }, { row => td[map checkbox('sel',undef,$_,$entry[$_][0]), 5..9] }, ], ); print header,$tmpl->output; __DATA__ <tmpl_loop result> you checked the <tmpl_var which> box<br /> </tmpl_loop> <form method="POST"> <table> <tmpl_loop checkbox> <tr><tmpl_var row></tr> </tmpl_loop> </table> <input type="submit" name="go" value="showme" /> </form>
Depending upon how much control your designers need, you can swing the pendulum in either direction when you combine HTML::Template with CGI.pm instead of using it as an alternative/replacement. In the case of a checkbox, there isn't much to change, and anything you can change should probably be done via CSS. But in the case of the table cells (<td> tags), there could be a lot to change and taking that away from the designers is probably not good. However, one could argue that the programmer should specify the CSS class of the <td> tag and allow the designers to define the CSS rules. In that case, mixing CGI.pm may not be a bad idea.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) 5Re: Converting large numbers of checkboxes to small number of params by jeffa
in thread Converting large numbers of checkboxes to small number of params by MrCromeDome

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-23 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found