in reply to Checkbox values

First, you may want to enclose your code in

<CODE></CODE>
tags.

The following script demonstrates how to use CGI.pm to create checkbox groups and retrieve checked values:

#!/usr/bin/perl -w use CGI qw/ :standard /; use strict; my @checkbox_items = qw/one two three four five/; print header, start_html( -title => 'Checkbox group sample using CGI.pm' ), start_form; # The following code creates the checkbox group using the elements in # @checkbox_items print checkbox_group( -name => 'testing', -values => \@checkbox_items, -linebreak => 1, ), submit, end_form; # Here we retrieve and output the user-selected values of the checkbox + group. my @checkbox_values = param( 'testing' ); print p( 'Check a button or two and click Submit Query to see the chec +ked values.' ), li( \@checkbox_values ), end_html; print end_html;

Update: Many thanks to George_Sherston++ for pointing out that I was importing the CGI functions twice. I've modified the code to use only :standard.

Update: Thanks to CharlesClarkson++ for noting that li accepts a list ref. Code modified.

Replies are listed 'Best First'.
Re: Re: Checkbox values
by George_Sherston (Vicar) on Nov 13, 2001 at 14:13 UTC
    I have the zeal of the convert about CGI.pm, and I think you were 100% right to offer this as the solution. The various difficulties that chriso got tangled in, and which the other posters help unpick, would have been avoided altogether by using CGI.pm... that's one of the great things about it, that other, far cleverer people than I, have thought about all the problems I'm going to encounter and provided ready-to-wear solutions, so I can get on with being creative.

    (Plus it's easier to read / debug by somebody else, it's quicker to type out, it's easier to cut 'n' paste into another script without disaster, you get no-brainer error-proof parameter parsing, you don't have to think about headers, you get support for CGI::Carp etc etc... in other words use CGI or die;)

    Speaking as someone who used to write his CGIs out longhand on the principle that "by the time I've learnt to use CGI.pm I could have written the script from scratch" I can report with hindsight that this attitude is b*ll*cks.

    I mention this in the hope of encouraging anyone who hasn't done so yet to make the jump.

    My only reservation abt your solution is that you import the standard methods, but then create a CGI object - which I think means effectively importing them twice, and certainly involves extra keystrokes :). It works just the same without any OO at all:
    #!/usr/bin/perl -w use CGI qw/ :standard /; use strict; my @checkbox_items = qw/one two three four five/; print header, start_html( -title => 'Checkbox group sample using CGI.pm' ), start_form; print checkbox_group( -name => 'testing', -values => \@checkbox_items, -linebreak => 1, ), submit, end_form; my @checkbox_values = param( 'testing' ); print p( 'Check a button or two and click Submit Query to see the chec +ked values.' ), li, join '<LI>', @checkbox_values; print end_html;


    § George Sherston
      Thanks for the feedback. I have used CGI.pm to create checkboxes. What I ran into with what you are suggesting is that the labels and values will be the same. If for example my label for an answer to a question is:
      BillJ may have been assigned as trustee to the security object which gives him supervisor rights over all NDS objects
      I don't want my value to this checkbox to be:
      BillJ may have been assigned as trustee to the security object which gives him supervisor rights over all NDS objects
      I want it to be 'd', or '3'. Something easier to compare with a correct answer. This also doesn't require as much memory. To use CGI.pm, I would have to set up my labels as a hash, which I'm trying to avoid doing because I haven't been able to get that to work. The problem I've been having using a hash is the syntax for the checkbox_group. It appears that I would have to set the value anyway and have the key in the hash match the value. This is very confusing so if you can explain this to me and show me how to write it that would be much appreciated.
        I do absolutely understand that feeling of "oh hell, I can't be bothered with this" that one gets when one Reads The Fine Manual... after all, the following sentence is scarcely written for the benefit of people who don't already pretty much know the score:
        The optional fifth argument is a pointer to an associative array relating the checkbox values to the user-visible labels that will be printed next to them (-labels).
        BUT... it is SO worthwhile getting through the pain barrier. It's seldom as tough as it looks, and once learned it saves time after time.

        Where it says
        a pointer to an associative array relating the checkbox values to the user-visible labels
        what it means is "set up a hash where the keys are what you want as the checkbox values, and the values are what you want the user to read" - i.e. something like:
        my %checkbox_items = ( a => 'BillJ may have been assigned as trustee to the security obje +ct which gives him supervisor rights over all NDS objects', b => 'BillJ may be wearing pink pyjamas when he comes round the mo +untain', c => 'BillJ may be the first man to orbit Mars on a unicycle', d => 'BillJ may be just another Perl hacker', );
        Now, to avoid confusion, you should probably construct your array of checkbox values from this hash - that way you're sure there's always going to be a match between the values and the labels. So do
        my @checkbox_values = keys %checkbox_items;
        Then you put it all together by adding the -labels argument when you call the checkbox_group method:
        print start_html, checkbox_group( -name => 'testing', -values => \@checkbox_values, -linebreak => 1, -labels => \%checkbox_items, ),
        And this will print out the following HTML (line breaks added for clarity:
        <input type="checkbox" name="testing" value="a" /> BillJ may have been assigned as trustee to the security object which g +ives him supervisor rights over all NDS objects <br /> <input type="checkbox" name="testing" value="b" /> BillJ may be wearing pink pyjamas when he comes round the mountain <br /> <input type="checkbox" name="testing" value="c" /> BillJ may be the first man to orbit Mars on a unicycle <br /> <input type="checkbox" name="testing" value="d" /> BillJ may be just another Perl hacker <br />
        Hope that does what you want to do in an intelligible way. One other thought - about saving memory. A point frequently made round here, for example by by dragonchild, is that Perl is optimised not for CPU or memory use, but for that scarcest and most valuable of computer consumables... developer time. I.e. YOUR time. So if a thing is quicker to write but slower to run, write it that way and then $RAM->buy('more');

        § George Sherston
Re: Re: Checkbox values
by CharlesClarkson (Curate) on Nov 14, 2001 at 19:12 UTC

    You had me till the very end. I really hate to mix HTML and CGI.

    print p( 'Check a button or two and click Submit Query to see the chec +ked values +.' ), li, join '<LI>', @checkbox_values; print end_html;
    print p( 'Check a button or two and click Submit Query to see the chec +ked values.' ); print li($_) for @checkbox_values; print end_html;


    Update: CGI.pm will allow li a list reference.
    print p( 'Check a button or two and click Submit Query to see the checke +d values.' ), li(\@checkbox_values), end_html;



    HTH,
    Charles K. Clarkson