在我的 Perl CGI 脚本中,如何访问复选框组中的选中值?

发布于 2024-12-08 12:13:35 字数 1262 浏览 0 评论 0原文

我有一个带有多种输入法的表单。我将该数据传递给 Perl 脚本,以根据数据输入处理打印输出文本。我对复选框组的值有疑问。 (复选框均具有相同的名称)

<td>Profiles: </td>
 <td><input type=\"checkbox\" value=\"oneconnect\" name=\profile[]\">OneConnect <br />
 <input type=\"checkbox\" value=\"http\" name=\profile[]\">HTTP <br />
 <input type=\"checkbox\" value=\"xforwardedfor\" name=\profile[]\">Xforwarded-for</td>
 </tr>
 </table>

该操作将此数据发送到 perl 脚本进行处理。

read (STDIN, $FormData, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $FormData);
print Dumper($FormData);

foreach my $pair (@pairs) {
# Separate the name and value:
($name, $value) = split(/=/, $pair);
# Convert + signs to spaces:
$value =~ tr/+/ /;
# Convert hex pairs (%HH) to ASCII characters:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Store values in a hash called %FORM:
$FORM{$name} = $value;
}

然后,我可以使用以下命令打印大部分数据:

print "$FORM{'vsip1'}  $FORM{'vsport1'} <br />\n";

但是,我无法使用相同的打印命令从复选框访问名称“profile”。当我“转储”数据时,我确实看到了“配置文件”值。 打印转储程序输出显示 (&%5Cprofile%5B0%5D%22=oneconnect&%5Cprofile%5B1%5D%22=http&SubmitForm) 这是哈希的哈希吗?以及如何在 perl 中访问这些值。

感谢您提供的任何建议。

I have a form with several input methods. I pass that data to a perl script to process print out text based on the data input. I am having a problem with the values of a checkbox group. (checkboxes all with the same name)

<td>Profiles: </td>
 <td><input type=\"checkbox\" value=\"oneconnect\" name=\profile[]\">OneConnect <br />
 <input type=\"checkbox\" value=\"http\" name=\profile[]\">HTTP <br />
 <input type=\"checkbox\" value=\"xforwardedfor\" name=\profile[]\">Xforwarded-for</td>
 </tr>
 </table>

The action sends this data to a perl script for processing.

read (STDIN, $FormData, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $FormData);
print Dumper($FormData);

foreach my $pair (@pairs) {
# Separate the name and value:
($name, $value) = split(/=/, $pair);
# Convert + signs to spaces:
$value =~ tr/+/ /;
# Convert hex pairs (%HH) to ASCII characters:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Store values in a hash called %FORM:
$FORM{$name} = $value;
}

I can then print most of the data the with:

print "$FORM{'vsip1'}  $FORM{'vsport1'} <br />\n";

However, I cannot access the name "profile" from the checkboxes with the same print command. I do see the "profile" values when I "dump" the data.
print dumper output shows (&%5Cprofile%5B0%5D%22=oneconnect&%5Cprofile%5B1%5D%22=http&SubmitForm) Is this a hash of a hash? and how can I access these values in perl.

Thank you for any suggestions you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

婴鹅 2024-12-15 12:13:35

您应该使用 CGICGI::Simple 所以你不必担心此类问题。

my @profile = $cgi->param('profile');

请参阅参数

You should use CGI or CGI::Simple so you don't have to worry about such matters.

my @profile = $cgi->param('profile');

See param.

巨坚强 2024-12-15 12:13:35

哇。这看起来确实是很多多余的工作。您没有使用网络服务器吗?

盒?看一下 uri_escape

如上所述,这是使用 CGI 的片段:

<html>
  <form <form name='formName' method='POST' action='someperlscript.pl'>
  <tr><td>
     <input type=checkbox name='XMatch' $xMatch> Exact Match
  </td></tr>
  </form>
</html>

use CGI;

my $q = new CGI;

$xMatch = $q->param( "XMatch" );

$xMatch = ( $xMatch eq "on" ? "checked" : "" );

...

Wow. That does look like a lot of excess work. Are you not using a web server?

Pack? Take a look at uri_escape.

As mentioned above, here's a snippet using CGI:

<html>
  <form <form name='formName' method='POST' action='someperlscript.pl'>
  <tr><td>
     <input type=checkbox name='XMatch' $xMatch> Exact Match
  </td></tr>
  </form>
</html>

use CGI;

my $q = new CGI;

$xMatch = $q->param( "XMatch" );

$xMatch = ( $xMatch eq "on" ? "checked" : "" );

...
素年丶 2024-12-15 12:13:35

看来上面的代码是为了从命令行进行测试而编写的。如果您创建并导出一个名为 CONTENT_LENGTH 的环境变量,并为它分配一个至少与您正在检查的名称-值对字符串一样长的数值,它会将它们转储为字符串(使用 Dumper),并且,如果名称对应于 $FORM{'vsip1'}、$FORM{'vsport1'} 等中的键,如果它们是文本框或始终返回值的其他 HTML 小部件,则应该打印它们。如果您在命令行上包含代表复选框的项目,并且包含值,它也会打印它。但是,如果您在 perl CGI 处理程序中解释来自 HTML 表单的查询字符串,则仅当复选框已被选中时,它才会包含复选框名称-值对。否则,它不会返回任何名称/值对。我使用 CGI 处理脚本在一个简短的 HTML 页面上对此进行了测试,这就是结果。

It appears that the above code was written to be tested from the command line. If you create and export an environment variable named CONTENT_LENGTH, and assign it a numeric value at least as long as the string of name-value pairs you are checking, it will dump them as a string (using Dumper), and, if the names correspond to the keys in $FORM{'vsip1'}, $FORM{'vsport1'}, and so forth, it should print them if they are text boxes, or some other HTML widget that always returns a value. If you include an item representing a check box on the command line, it prints it as well, if it includes a value. However, if you are interpreting a query string from an HTML form within a perl CGI handler, it will only include the checkbox name-value pair if the checkbox has been checked. Otherwise, it does not return any of that name-value pair. I tested this on a short HTML page with a CGI processing script, and this was the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文