Perl/CGI 表单检查

发布于 2024-12-18 09:38:22 字数 2273 浏览 1 评论 0原文

这是我第一次尝试 CGI(我懂一些 Perl),但我摔倒了。

我想输入一个表单并检查它 - 但检查部分根本看不到提交的值。

我直接作为 http://example/cgi-bin/formcheck.cgi 运行它 - 没有 HTML 调用它。

我怀疑它正在运行,从底部掉下来,然后每当按下按钮时就从头开始运行。但我不确定。这是我的代码:

#!/usr/bin/perl -w
use strict; # let's tighten things up

use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

print header;
print start_html("form check");
print "<h1>form check</h1>\n";

print_questions();

print_checks();
print "<hr>\n";

print end_html;

sub print_questions {

  if ( !defined(param('action')) || param('action') eq 'New' ) {

    my $p = int(rand(10));          # fix error under strict
    my $q = int(rand(10));          # fix error under strict
    my $i = 0;                      # fix error under strict

    my @question = '';              # fix error under strict
    my @answer   = '';              # fix error under strict

    $question[$i] = "$p X $q =";
    $answer[$i]   =  $p * $q;

    print start_form;
    print "$question[$i]";
    print textfield(-name=>'response',-default=>'',-size=>3);
    print "<p>";
    print submit('action','New');
    print submit('action','Check');
    print end_form;
    print "<p>";

    param(-name=>'question',-value=>@question);
    param(-name=>'answer',-value=>@answer);
    print "<hr>\n";
  }

}

sub print_checks {

  if ( param('action') eq 'Check' ) {

    my $errors = 0;                    # fix error under strict
    my $i = 0;                         # fix error under strict

    my @question = param('question');  # fix error under strict
    my @answer   = param('answer');    # fix error under strict
    my @response = param('response');  # fix error under strict

    if ( $answer[$i] != $response[$i] ) {

      $errors++;
      print "<font color=#FF0000>";
    } else {
      print "<font color=#00FF00>";

    }

    print "$question[$i] = $answer[$i]";
    print "</font>";

    print "<p>";
    print start_form;
    print submit('action','New');
    print end_form;
    print "<p>";
    print "<hr>\n";

    if ($errors == 0) {

      print "CORRECT!<br>";
    } else {
      print "NOPE!<br>";

    }
  }
}

非常感谢任何帮助

This is my first attempt at CGI (I know some Perl) but i'm falling on my face.

I want to enter a form and check it - but the check section isn't seeing the submitted values at all.

I am running this directly as http://example/cgi-bin/formcheck.cgi - there is no HTML calling this.

I suspect it's running, dropping out the bottom and then running from scratch whenever a button is pressed. I'm not sure though. Here's my code:

#!/usr/bin/perl -w
use strict; # let's tighten things up

use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

print header;
print start_html("form check");
print "<h1>form check</h1>\n";

print_questions();

print_checks();
print "<hr>\n";

print end_html;

sub print_questions {

  if ( !defined(param('action')) || param('action') eq 'New' ) {

    my $p = int(rand(10));          # fix error under strict
    my $q = int(rand(10));          # fix error under strict
    my $i = 0;                      # fix error under strict

    my @question = '';              # fix error under strict
    my @answer   = '';              # fix error under strict

    $question[$i] = "$p X $q =";
    $answer[$i]   =  $p * $q;

    print start_form;
    print "$question[$i]";
    print textfield(-name=>'response',-default=>'',-size=>3);
    print "<p>";
    print submit('action','New');
    print submit('action','Check');
    print end_form;
    print "<p>";

    param(-name=>'question',-value=>@question);
    param(-name=>'answer',-value=>@answer);
    print "<hr>\n";
  }

}

sub print_checks {

  if ( param('action') eq 'Check' ) {

    my $errors = 0;                    # fix error under strict
    my $i = 0;                         # fix error under strict

    my @question = param('question');  # fix error under strict
    my @answer   = param('answer');    # fix error under strict
    my @response = param('response');  # fix error under strict

    if ( $answer[$i] != $response[$i] ) {

      $errors++;
      print "<font color=#FF0000>";
    } else {
      print "<font color=#00FF00>";

    }

    print "$question[$i] = $answer[$i]";
    print "</font>";

    print "<p>";
    print start_form;
    print submit('action','New');
    print end_form;
    print "<p>";
    print "<hr>\n";

    if ($errors == 0) {

      print "CORRECT!<br>";
    } else {
      print "NOPE!<br>";

    }
  }
}

Any help greatly appreciated

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

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

发布评论

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

评论(2

水波映月 2024-12-25 09:38:22

好吧,再读一遍你的代码:

你设置了一个问题和答案参数,但他们没有
表格中的任何表示形式。你需要一个隐藏的()字段
保存它们。

Ok, read your code again:

You set an question and answer parameter, but they do not have
any representation in the form. You need a hidden()-field to
preserve them.

禾厶谷欠 2024-12-25 09:38:22
print textfield(-name=>'response',-default=>'',-size=>3);
print hidden(-name=>'question',-value=>@question);         # this fixes my bug
print hidden(-name=>'answer',-value=>@answer);             # this fixes my bug

我还必须删除这些行:

param(-name=>'question',-value=>@question);
param(-name=>'answer',-value=>@answer);

再次感谢。

print textfield(-name=>'response',-default=>'',-size=>3);
print hidden(-name=>'question',-value=>@question);         # this fixes my bug
print hidden(-name=>'answer',-value=>@answer);             # this fixes my bug

I also have to remove these lines:

param(-name=>'question',-value=>@question);
param(-name=>'answer',-value=>@answer);

Thanks again.

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