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

HI, I have been writing a CGI script that prodeuces a table with 8 rows and 12 columns. I have embedded each cell with a radio button to click them ON. So, apparently I have got 96 radio buttons in my page. The specialty(disadvantage?) with radio button we cant click more than one radio button with same 'name'

In my case I should be able to check more than one radio button. So I have got different names for each button. The bit of code of that does this is something like this:
foreach my $cell (@rows){ split(/,/); print $cgi->td("$cell"); for(my $i= 1;$i<=12;$i++){ print $cgi->td($cgi->radio_group(-name=>"radio$cell$i +", -values=>['ON'], -default=>'', -column=>2, -row=>1)) ; } print $cgi->Tr(); }

where @rows is the list of row names (which is A-H). So, I have got radioA1..A12 for the first row, B1..B12 for the second row and so on. It all is fine till here, the problem now is, how could I check(whether any parameters are entered and if not report the viewer to click any buttons to work on) and/or get those parameters that has been clicked ON.

I have processes depending upon them switching ON or OFF. I have got a command line script for these processes. All I have to do is to get the parameters that are requested for each cells. Is there any thing to get all the parameters that has been entered in the page? and then walking through the array? Or is there any better way of doing this?

Any suggestions please,
Thanks a lot in advance.

Replies are listed 'Best First'.
Re: Params in CGI
by ccn (Vicar) on Nov 20, 2008 at 11:21 UTC
    perldoc CGI

    foreach my $cell ('A' .. 'H') { foreach my $i (1 .. 12) { my $name = 'radio' . $cell . $i; print "Param $name missing\n" unless $cgi->param($name); } }

    [OT] You have 96 required options on a page, and you may consider to reset the whole form on any error input to make user's life yet more harder

      Hi, but my question is the $name that you have initialized won't have scope outside this loop. I need to check the parameters before(to check whether the form has parameters) and after (to process) this loop. Any suggestions?

      Thanks

        Parameters are checked/validated _inside_ of that loop. $cgi->param('radioA1') returns false if that radio was not checked.

Re: Params in CGI
by Corion (Patriarch) on Nov 20, 2008 at 10:06 UTC
Re: Params in CGI
by bart (Canon) on Nov 20, 2008 at 11:56 UTC
    In my case I should be able to check more than one radio button. So I have got different names for each button.
    Methinks you shouldn't be using radio buttons, but check boxes. Radio buttons not only have the disadvantage you mentioned, but once they're on, the user can't turn them off again. Ouch.

    Make them checkboxes and you can give them the same name; use row name and column letter for the value. And your problem is solved.

      Also, How could I get the list of check boxes that has been checked in this? (If I manage the people to like checkboxes??)
        my @checked = grep { param($_) eq 'ON' } param();
      Thats true, But I have to use the radio buttons :( the lab people here do like radio buttons more than the checkboxes :(

        Seriously? Your going to use a broken interface because they like it better? The fact that you can't un-check them is huge and i would highly recommend you use check boxes, you can almost certainly make them do what you want. BTW I beleive that if you have more than one value for a given form element then it will return an array see CGI Docs for more info.


        ___________
        Eric Hodges
Re: Params in CGI
by Your Mother (Archbishop) on Nov 20, 2008 at 18:56 UTC

    Echo above sentiments: radio is completely wrong for this. You want checkboxes. Here's a snippet to play with. Sorry it's not adapted to your code above but I did it for fun. :) You can swap CGI for CGI::Pretty. The only difference is the output is better for human consumption with the pretty version.

    use strict; use warnings; use CGI::Pretty; my $cgi = CGI::Pretty->new(); print $cgi->header(), $cgi->start_html(); if ( my @selected = $cgi->param("catzez") ) { print $cgi->h1("You be checkin..."); print $cgi->ul( $cgi->li( \@selected ) ); } else { print $cgi->h1("You chex sumdings, pleez."); } print $cgi->start_form(), $cgi->checkbox_group( -name => "catzez", -cols => 3, -values => [ "Oh, hai!", "Oh, NOES!", "Oh, Pl +eaze!", "I can haz PurlMunkz?", "Hacks. I gots dem.", "PERL. Yur doin it rong.", "Golf. Yer doin it... Pretty wel +l akshully.", "Wherz mah buckit?", "They takes + mah buckit!" ], ), $cgi->submit("Go, goez!"); $cgi->end_form(), $cgi->end_html();