http://qs1969.pair.com?node_id=272840


in reply to Re: Converting large numbers of checkboxes to small number of params
in thread Converting large numbers of checkboxes to small number of params

try this for fun - it accepts the text box values as anrray #!/usr/bin/perl ## relatively painless iteration over multiple selection boxes use CGI qw(:standard); @sel = param('sel'); @entry = ("first", "second", "third", "fourth", "fifth", "sixth", "sev +enth", "eighth", "ninth", "tenth"); print "Content-Type: text/html\n\n"; if ($sel[0] ne '') { # do database stuff here (each entry in the sel array is the index to + your array of data for the database) ## just for fun show boxes checked foreach $ent (@sel) { print "you checked the $entry[$ent] box<br>\n"; } } print <<EOF <form name=fred action=jd.cgi> <table> <tr> <td> one <input type=checkbox name=sel value=0> </td> <td> two <input type=checkbox name=sel value=1> </td> <td> three <input type=checkbox name=sel value=2> </td> <td> four <input type=checkbox name=sel value=3> </td> <td> five <input type=checkbox name=sel value=4> </td> </tr> <tr> <td> six <input type=checkbox name=sel value=5> </td> <td> seven <input type=checkbox name=sel value=6> </td> <td> eight <input type=checkbox name=sel value=7> </td> <td> nine <input type=checkbox name=sel value=8> </td> <td> ten <input type=checkbox name=sel value=9> </td> </tr> </table> <input type=submit value=showme> </form> EOF Hope this helps John
  • Comment on Re: Re: Converting large numbers of checkboxes to small number of params
  • Download Code

Replies are listed 'Best First'.
(jeffa) 3Re: Converting large numbers of checkboxes to small number of params
by jeffa (Bishop) on Jul 09, 2003 at 23:09 UTC
    Ughh! If you are going to use CGI, then use CGI!!
    #!/usr/bin/perl -T use strict; use warnings; use CGI::Pretty qw(:standard); 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)], ); print header, start_html('test'); print map "$entry[$_][1] box checked".br, param 'sel' if param 'go'; print start_form('fred'), table( Tr(td[map checkbox('sel',undef,$_,$entry[$_][0]), 0..4]), Tr(td[map checkbox('sel',undef,$_,$entry[$_][0]), 5..9]), ), submit(-name=>'go',-value=>'showme'), end_form, end_html, ;
    ;)

    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)
    
      More often than not I believe that CGI's html-generating methods are not a good approach.

      Of course I also don't have a single answer that I would suggest in place, there are many ways of generating HTML and they each are appropriate in different places. For more detailed thinking on the factors that I consider, please see Re (tilly) 6: Code Critique.

        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:

        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)