如何以cgi形式将数组变量打印为复选框名称? [英] How to print the variables of an array as checkbox names in a cgi-form?

查看:72
本文介绍了如何以cgi形式将数组变量打印为复选框名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来以名称为数组变量的cgi形式打印复选框?

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

推荐答案

您有几个问题。


  1. 如果要将多个值传递给哈希中的单个属性,则需要传递参考。您的代码(在未记录修改的宽限期内从问题中将其删除之前)将解释为: checkbox(-values-> apple, orange => berry)

  2. 复选框仅取一个值。如果要传递多个,则需要多次调用

  3. CGI.pm的 HTML生成功能将不再使用。改为使用模板系统。

  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.

例如:

#!/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.

这篇关于如何以cgi形式将数组变量打印为复选框名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆