Perl/CGI 表单检查
这是我第一次尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,再读一遍你的代码:
你设置了一个问题和答案参数,但他们没有
表格中的任何表示形式。你需要一个隐藏的()字段
保存它们。
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.
我还必须删除这些行:
再次感谢。
I also have to remove these lines:
Thanks again.