Is there a simple way to print check boxes in an cgi form whose names are variables of an array?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You have several problems.
- If you want to pass multiple values to a single property in a hash, then you need to pass a reference. Your code (before you deleted it from the question during the grace period when edits are not recorded) will be interpreted as:
checkbox("-values" -> "apple", "orange" => "berry")
. checkbox
only takes one value. If you want to pass multiple, then you need to call it multiple times- CGI.pm's HTML generation functions should no longer be used. Switch to a template system instead.
For example:
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use Template;
my @checkbox_values = qw[apple orange berry];
my $template = q[
Example template
[% FOR item IN checkbox_values %]
<input type="checkbox" name="example" value="[% item | html %]">
[%- END %]
];
print Template->new()->process(\$template, { checkbox_values => \@checkbox_values })
NB: Templates are better stored in separate files.