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. | [reply] [Watch: Dir/Any] [d/l] [select] |
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 | [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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. | [reply] [Watch: Dir/Any] [d/l] [select] |
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 | [reply] [Watch: Dir/Any] [d/l] [select] |
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
| [reply] [Watch: Dir/Any] [d/l] |