简体   繁体   English

满足条件时如何使CGI输入字段返回其初始状态(CGI.pm/Perl)

[英]How to let CGI input field return to its initial state when a condition is fulfilled (CGI.pm/Perl)

I have a CGI script that looks like this. 我有一个看起来像这样的CGI脚本。

#!/usr/local/bin/perl

use CGI ':standard';

print header;
print start_html('A Simple Example'),
h1('A Simple Example'),
start_form,
"What's your name? ",textfield('name'),
p, submit, end_form,
hr;

my %unwantedwords = {'foo' => 1 };

if (param())
{
    my $text =param('name');

    # I attempted this to but failed.
    unless ($unwantedwords{$text}){
        print  "Your name is: ",$text,
   }
    hr;
}
print
end_html;

What I want to do is basically to receive a text via 'textfield' and then print it out on the web. 我要做的基本上是通过“文本字段”接收文本,然后将其打印在网络上。 But when a word inserted by user is an unwanted words (stored in the hash), instead of printing it I'd like the web to return to it's fresh initial state. 但是,当用户插入的单词是不需要的单词(存储在哈希中)时,我希望网络返回到它的新鲜初始状态,而不是打印出来。

What's the best way to do it? 最好的方法是什么? The above code doesn't work. 上面的代码不起作用。

Something like, (Untested).. 诸如(未经测试)之类的东西。

use strict;
use warnings;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );

my @unwanted = qw( foo bar baz );

my $text = param('name');

print header,
      start_html('A Simple Example');

display_form() and exit unless !grep($text eq $_, @unwanted);

print "Hello $text\n";

sub display_form {
   print start_form,
         h1('A Simple Example'),
         qq( What's your name? ), textfield(-name => 'name', -value => '', -override => 1), p,
         submit, hr,
         end_form;
}

print end_html;

You'll need to save the state of the word before it is submitted, and retrieve it to send back if the submitted word is on your list of bad words. 您需要先保存单词的状态,然后再提交,如果提交的单词在您的不良单词列表中,则将其检索以发送回去。

Your implementation will differ depending on your persistence engine, but regardless of whether you use a cookie to store the old word, or use a session store, you'll do something like this: 您的实现会因持久性引擎而异,但是无论您是使用Cookie存储旧单词还是使用会话存储,您都将执行以下操作:

 1. store the old word

 2. send the old word along with the web form.

 3. receive new word back

 4. if (new word is in the bad word list) {
      get the old word from storage
    }
    else {
      store the new word
    }

 5. do what comes next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM