Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

perl - How to print the variables of an array as checkbox names in a cgi-form?

Is there a simple way to print check boxes in an cgi form whose names are variables of an array?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have several problems.

  1. 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").
  2. checkbox only takes one value. If you want to pass multiple, then you need to call it multiple times
  3. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...