【问题标题】:How can I store the parameters passed in multiple requests to a same CGI?如何将多个请求中传递的参数存储到同一个 CGI?
【发布时间】:2021-06-25 08:34:27
【问题描述】:

我有两个不同的 Perl CGI,如下所示,它们创建了两个 HTML。

tes1.pl 是从 test.html 调用,作为带有参数的 POST 调用。
test2.pl 将从 test1.pl 中调用。但问题出在这里,test2.pl 中有一个指向 test1.pl 的链接。
所以。单击链接时,所有 POST 参数都将消失,因为它在不同的上下文中运行。
有人可以告诉我如何解决这种情况吗?

test.html:

<form id = "test" action ="./tes1.pl" method="POST" target="_blank">
  <input type="hidden" id='param_sender' name="param1" value="">
 </form>
 <a onclick="setValue();document.getElementById('test').submit();">SEND THE VALUE</td></tr>

test1.pl:

use CGI;
$page=CGI::new();
my $method=$page->request_method;
if ($method eq "POST") {
  my $gpmsc_user=$page->param('param1');
}
print $page->header,
"<link rel='stylesheet' href='./test.css' type='text/css'>";
print $page->start_html(-title=>"TEST1");

test2.pl

my $page= new CGI();
print $page->header,
"<link rel='stylesheet' href='./test.css' type='text/css'>";
print $page->start_html(-title=>'TEST2');
print $page->startform(-method=>post,-action=>'test3.pl');
print $page->append,
"<table border=true>
 <tr class=amhead>
  <td colspan=3>
  <br><a href='test1.pl'>Return to Test1</a>
  </td>
 </tr>..."

【问题讨论】:

  • 你的例子真的很混乱。你没有表现出任何的召唤。我认为您的意思是用户在不同的脚本之间跳转。如果你必须做 CGI,看看 CGI::Session。您希望跨多个请求在后端保留每个用户的数据,因此您需要一个会话。
  • test.html 有一个锚标记来提交调用 test.pl 的表单 test2.pl 有一个锚标记,其中有指向 test1.pl 的链接。这是你指的吗?但是,你说得对,我需要在对同一个 CGI 的多个请求中保留数据

标签: perl cgi


【解决方案1】:

您没有两个相互调用的 CGI 程序。您有几个输出 HTML 表单的 Perl 程序。因此,如果您希望一个表单在表单中具有相同的参数,则必须以将值设置为这些输入的默认值的方式创建 HTML。

使用现已弃用的 CGI 功能,

use CGI qw(:html :form);

my $cgi = CGI->new( 'veggie=tomato' ); # from first form

my $field = 'veggie';
my $value = $cgi->param(
    -name  => $field,
    );

print textfield(  # create second form
    -name    => $field,
    -default => $value,
    );

这个输出:

<input type="text" name="veggie" value="tomato" />

但是,正如 CGI 文档所指出的,这些 HTML 生成器已被弃用,不应使用。文档列出了一些替代方案。

【讨论】:

  • 确实如此。我需要重新措辞他们正在创建两个不同的 HTML。 test1.pl 可以从#1 test.html 和#2 test2.pl 调用,我如何存储#1 发送的参数并在#2 调用时使用它们。
猜你喜欢
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
  • 2019-09-29
  • 2022-11-01
  • 1970-01-01
相关资源
最近更新 更多