【问题标题】:How can I process element values in Perl CGI?如何在 Perl CGI 中处理元素值?
【发布时间】:2012-12-18 23:03:58
【问题描述】:

我想将选定单选按钮的 id 名称和一些 div 元素数据传递到服务器并在服务器端处理这些数据。

例如,下面是我的 CGI 代码。

use strict;
use CGI;
print

"Content-type:text/html\r\n\r\n",
"<html>",
"<body>",
"<div style='float:left'>",
"<input type='radio' name='selections' id='red'>",
"<input type='radio' name='selections' id='blue'>",
"<input type='radio' name='selections' id='green'>",
"</div>",
"<div style='float:left'>",
"<div style='float:left' id='redtime'>red time</div>",
"<div style='float:left' id='blutime'>blue time</div>",
"<div style='float:left' id='greentime'>green time</div>",
"</div>",

"<input type='button' value='clickme'/>",
"</body>",
"</html>";

在上面的 CGI 文件中,我需要传递所选单选按钮的 id 值和任何 div 元素的数据。我如何在 CGI 中做到这一点?

【问题讨论】:

标签: perl cgi


【解决方案1】:

id 用于客户端。如果你想在服务器端获取它,那么你需要使用 JavaScript 来生成隐藏输入或使用 XMLHttpRequest 发出请求。

请改用value

那么你就:

my $value = $instance_of_CGI->param('name_of_radio_group');

您还需要将提交按钮设置为type="submit"(提交表单)而不是type="button"(除非您将JavaScript 绑定到它,否则它什么也不做)。


您的表单控件还需要放置在&lt;form&gt; 元素中,并将action 属性设置为要处理数据的脚本的URI。


我还建议您将所有表单标签放在与它们相关的输入旁边在标记中,而不仅仅是使用 CSS,并且您对它们使用 &lt;label&gt; 元素,而不是 &lt;div&gt;元素。


"Content-type:text/html\r\n\r\n",

不要手动执行此操作。您正在使用 CGI.pm。

print $instance_of_CGI->header();

【讨论】:

  • 嗨 Quentin,如果我想在提交之前验证广播组,例如是否选择了任何广播,我该如何调用 javascript?
  • @Madhankumar — 这明显超出了您原始问题的范围,应该在新问题中而不是在 cmets 中处理……除了它也是一个常见问题,因此您应该能够通过以下方式解决它搜索。
猜你喜欢
  • 1970-01-01
  • 2012-11-06
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多