【发布时间】:2015-11-17 20:47:51
【问题描述】:
我有一个类似调查的简单表单,我使用 Perl CGI 来执行此操作,不会使用交互式 HTML 文档。我遇到的问题是我不知道如何确定单击了哪个提交按钮,如果是,请继续下一个问题或转到上一个问题。我已经为按钮设置了值,但我不确定如何使用该值来继续下一个问题。 (我是 Perl CGI 的新手,请原谅草率的代码。谢谢)
#!c:/Perl/bin/perl.exe
use CGI;
use CGI::Carp qw(fatalsToBrowser);
if ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
$form_size = $ENV{'CONTENT_LENGTH'};
read( STDIN, $form_data, $form_size );
} else {
$form_data = $ENV{'QUERY_STRING'};
}
$form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
@fields = ( split( /&/, $form_data ) );
$size = @fields;
for ( $i = 0; $i < $size; $i++ ) {
( $key, $value ) = ( split( /=/, $fields[$i] ) );
$value =~ s/[;<>\(\)\{\}\*\|'`\&\$!#:"\\]/\ /g; # Syntax Highlighting fix: '
$value =~ s/[+]/\ /g;
$my_hash{$key} = $value;
}
my $q = new CGI;
print $q->header("text/html"), $q->start_html( -title => "Survey", -bgcolor => "blue" );
@labels = ( "Not at all", "", "Somewhat", "", "Extremely" );
@labels2 = ( "No", "Yes", "N/A" );
print $q->submit( 'prev_button', 'Prev' );
print $q->submit( 'next_button', 'Next', 'Javascript: validate_form()' );
if ( submit . value == Prev ) {
if ( $size == 0 ) {
$size = 0;
} else {
$size = $size - 1;
}
} elsif ( submit . value == Next ) {
if ( $size == 6 ) {
$size = 6;
} else {
$size + 1;
}
}
if ( $size == 0 ) {
print $q->p("1. Please enter your name:");
print $q->textfield( 'q1', '', 50, 80 );
} elsif ( $size == 1 ) {
print $q->p("2. Did you find the information included within this web site useful?");
print $q->radio_group( 'q2', [ '1', '2', '3', '4', '5' ], "-", 'false', \%labels );
} elsif ( $size == 2 ) {
print $q->p("3. Is the web site easy to navigate?");
print $q->radio_group( 'q3', [ '1', '2', '3', '4', '5' ], "-", 'false', \%labels );
} elsif ( $size == 3 ) {
print $q->p("4. Were you able to find the information you were looking for?");
print $q->radio_group( 'q4', [ '1', '2', '0' ], "-", 'false', \%labels2 );
} elsif ( $size == 4 ) {
print $q->p("5. What other information would you like to be included in the web site?");
print $q->textarea( 'q5', '', 10, 50 );
} elsif ( $size == 5 ) {
print $q->p("6. What suggestions you might have for our website improvement?");
print $q->textarea( 'q5', '', 10, 50 );
} elsif ( $size == 6 ) {
print $q->p("Thank you for your input.");
}
print $q->end_html;
exit(0);
【问题讨论】:
-
submit . value不是 Perl 语法。 -
您应该将
use strict; use warnings;添加到您的脚本顶部以及您编写的每个Perl 脚本中;它们可以帮助捕获许多简单的错误。详情请见Why use strict and warnings?。 -
哇,非常感谢。我对这门语言还很陌生,而且要求使它变得更加困难,因为我不能使用 Javascript,而且就像你说的那样,它是一种死语言。关于这段代码,我还有几个问题。虽然我现在已经对我的 $size 变量设置了限制,但是如何让表单在我的嵌套 If 语句中在提交按钮的咯咯声中生成下一个问题?看起来应该相对简单,但我卡住了。
标签: perl cgi perl-module