Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Checkbox values

by chriso (Sexton)
on Nov 13, 2001 at 02:57 UTC ( [id://124933]=perlquestion: print w/replies, xml ) Need Help??

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

I've set up the following loop to print a set checkboxes:

foreach $counter (0 .. $#questions) { if ($radio_checkbox[$counter] eq "c") { print "$questno[$counter]. $questions[$counter] <BR>"; @labels = split (/,/, $selections[$counter]); foreach $counter2 (0 .. $#labels) { print '<BR><INPUT TYPE="checkbox" NAME="checkbox" VALUE=$counter2>$labels[$counter2]'; } print br; }
Here is a sample of my output:
1. What are three items found in the kitchen? $labels[$counter2] $labels[$counter2] $labels[$counter2] $labels[$counter2] $labels[$counter2] $labels[$counter2]
The checkboxes themselves are coming out ok, even though this does not show them. However, neither the value for $labels or the value for $counter2 is being printed. How can I set up checkboxes with various values and labels based on an array? Thanks. BTW, I couldn't get the brackets to print around my array elements for this post.

Edit Masem - CODE tags and formatting

Replies are listed 'Best First'.
Re: Checkbox values
by Jazz (Curate) on Nov 13, 2001 at 03:25 UTC

    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.

      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.

      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
Re: Checkbox values
by dondelelcaro (Monk) on Nov 13, 2001 at 03:39 UTC
    Your problem is the ' instead of ".
    This line:
    print '<BR><INPUT TYPE="checkbox" NAME="checkbox" VALUE=$counter2>$labels[$counter2]';
    should be
    print qq(<BR><INPUT TYPE="checkbox" NAME="checkbox" VALUE=$counter2>$labels[$counter2]);
    q() or ' doesn't interpolate, whereas qq() or " does.
Re: Checkbox values
by mandog (Curate) on Nov 13, 2001 at 03:41 UTC
    print '<BR><INPUT TYPE="checkbox" NAME="checkbox" VALUE=$counter2>$labels[$counter2]';
    One problem you have is that you are using ' ' and not " ". Your $counter2 and $labels$counter2 are not being interpolated.

    If you need to nest quotes: you should escape the inside qoutes:

    print "\"\"\n";


    email: mandog
Re: Checkbox values
by mkmcconn (Chaplain) on Nov 13, 2001 at 03:52 UTC
    chriso,
    I believe that it's simply a matter of your variables being surrounded by single quotes.
    # this will expand the variables print "$labels[$counter]"; #this will print the literal variables, no expansion: print '$labels[$counter]';

    mkmcconn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://124933]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-03-28 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found