【问题标题】:Perl CGI - How can I delete contents of text fields?Perl CGI - 如何删除文本字段的内容?
【发布时间】:2012-05-13 19:11:05
【问题描述】:

所以,我对 Perl 中的 CGI 编程完全陌生。 问题很简单。是否有机会删除 CGI 中文本字段的内容?

我必须编写一个包含一些弹出菜单、提交按钮和文本字段(区域)的代码。

当我单击提交按钮时,程序会从其中一个 popup_menu 中读取值。 任务是将此内容复制到文本字段中,然后当我从 popup_menu 中选择另一个元素(当然点击提交按钮)时,让新内容写入文本字段替换旧内容。

我认为 perldoc.perl.org 只提供了一些关于 CGI 编程的信息。我会有很多问题... :(

任何帮助都是合适的!

【问题讨论】:

  • 这毫无意义。提交表单后,如何“从 popup_menu 中选择另一个元素”?那时没有菜单,甚至没有 HTML 页面。
  • Perl 是服务器端。 Javascript 是客户端。
  • 我没有遵循你的工作流程,但删除 CGI 对象的内容可以这样: $cgi->param(-name=>'foo', -value=>' ');

标签: perl cgi fastcgi cgi-bin


【解决方案1】:

我猜,您所描述的是:当您单击提交按钮时,您的 cgi 脚本将运行,给定您在表单中输入的参数。然后我要做的是:写回一些东西并再次打印表单 - 使用不同的值。

因此,即使这不是做此类事情的完美方式(对于简单的表单元素替换,您应该在客户端进行并使用 javascript - 您不需要 cgi 后端脚本),让我们看看如何一个 cgi 脚本可能看起来像。

首先,重要的是要知道如何编写表单。让我们假设您用 print 以“艰难的方式”编写它。 您的脚本要做的是解析输入,然后将其作为值添加到输出中。

use CGI;
my $q = CGI->new;

# get the value from the popup / html select
my $popup_value = $q->param('popup_menu'); # name of the <select name="..."> in your html
# ...

# writing the form
print $q->header;
# some more prints with form etc.
print textarea( -name    => 'text_area',
                -default => $popup_value // '', # will use empty string on first call
              );
# Don't turn off autoescaping !

顺便说一句,选择选项的值是一个简短的指示符,而不是完整的文本(即使这可能达到一定数量的字符)。因此,您可能会考虑使用要在文本区域中打印的适当值构建散列或数组,并为您的选择选项提供值 0、1、2 ...

my @text_values = ('', 'First text', 'second text', 'third text');
my $popup_value = $q->param('popup_menu') || 0; # default index.
# now use 1,2,3, ... as values in your popup_menu options
# ...
print textarea( -name    => 'text_area',
                -default => $text_values[$popup_value] );

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2011-08-02
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    相关资源
    最近更新 更多