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


in reply to Converting large numbers of checkboxes to small number of params

Would something more like this do what you are looking for? I do not usually like to mix using CGI to write out the tags, and writing them myself... But this should at least be on the right track.

Instead of having 100 differently named checkboxes, why not just use 1 checkbox name for each day? All the boxes that are checked will be read by CGI as an array of the values of the boxes that were checked.

#! /usr/bin/perl use strict; use CGI; my @days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); my @hours = ( "7am-8am", "8am-9am", "9am-10am", "10am-11am" ); my $q=CGI->new(); print $q->header, $q->start_html; print $q->start_form; print '<table><tr>', $q->td(); for my $hour (@hours) { print $q->td($hour); } print '</tr>'; for my $day (@days) { print '<tr>'; print '<td>', $day, '</td>'; for my $hour (@hours) { print $q->td($q->checkbox(-name=>$day, -value=>$hour, -label=>'' ) ); } print '</tr>'; } print '</table>'; print $q->submit; print $q->end_form; # Display the values to make sure it's doing the right thing print '<br>Values:<br><br>'; for my $day (@days) { print $day, '<br>'; print join('<br>', $q->param($day)); print '<br><br>'; } print $q->end_html;
  • Comment on Re: Converting large numbers of checkboxes to small number of params
  • Download Code

Replies are listed 'Best First'.
(jeffa) 2Re: Converting large numbers of checkboxes to small number of params
by jeffa (Bishop) on Jul 09, 2003 at 23:45 UTC
    "I do not usually like to mix ... But this should at least be on the right track."

    I don't like to mix hardcoded HTML tags and CGI.pm methods either, but i like what your code does. :) Here is another version that only uses CGI.pm:

    #!/usr/bin/perl -T use strict; use warnings; use CGI::Pretty qw(:standard); my @hour = qw(7am-8am 8am-9am 9am-10am 10am-11am); my @day = qw( Monday Tuesday Wednesday Thursday Friday Saturday Sunday ); print header, start_html, start_form, table( Tr(th['&nbsp;',@hour]), map { my $day = $_; Tr( th({align=>'right'},$_), td[map checkbox("_$day",0,$_,''), @hour], ) } @day ), submit, end_form, h2('Values'), ul( map { li(substr($_,1,length $_)) . ol(li[param($_)]) } grep {/^_/} param(), ), end_html, ;
    By appending an underscore to each of the names of the check boxes, i can extract them out quite easily. This does, however, complicate things when displaying those names (hence the substr call). Hope this helps. :)

    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)
    
Re: Re: Converting large numbers of checkboxes to small number of params
by Anonymous Monk on Jul 13, 2003 at 07:37 UTC
    Just thought I'd point out some features of CGI.pm
    use CGI; die CGI->start_table, CGI->start_Tr, CGI->end_Tr, CGI->end_table; __END__ <table><tr></tr></table> at - line 2.