如果用户没有 select 单选按钮 Perl/CGI,我该如何发送错误消息
How can I send error message if user doesn't select a radio button Perl/CGI
尝试编写我的第一个 Perl/CGI 脚本并将 运行 用于某些领域,我不太了解如何继续前进。创建了一个 HTML 页面,其中包含用户输入并发送到脚本的信息。如果用户不遵循指示,该脚本会在第二页上将信息输出回错误给用户。我的表单如下所示:
- 项目编号:用户输入一个数字
- 产品名称:
- 产品成本:
- 售价:
产品类别:
- 单选按钮 1
- 单选按钮 2
- 单选按钮 3
手头数量(输入字段)
提交按钮
如果用户没有 select 单选按钮,我如何测试并发送错误
我想要 return 应该由脚本在第二页上自动生成的利润金额(价格 - 成本)。到目前为止,这是我的脚本(我使用记事本++作为编辑器)
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);
local ( $buffer, @pairs, $pair, $name, $value, %FORM );
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
}
else {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split( /&/, $buffer );
foreach $pair ( @pairs ) {
( $name, $value ) = split( /=/, $pair );
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex())/eg;
$FORM{$name} = $value;
}
#variables for input text fields
$Item = $FORM{Item};
$Name = $FORM{Name};
$Cost = $FORM{Cost};
$Price = $FORM{Price};
$Quantity = $FORM{Quantity};
#Radio button
$letter = $FORM{letter};
#Validate data is entered in first 2 input boxes
if ( $Item eq "" ) {
print "$Item Field Cannot be Blank";
}
if ( $Name eq "" ) {
print "$Name Field cannot be Blank";
}
#Validate the correct amount is entered
if ( $Cost >= .50 && $Cost <= 1000 ) {
print "$Cost Cost must be between $.50 and 00";
}
if ( $Price >= 1.00 && $Cost <= 2000 ) {
print "$Price Price must be between .00 and 00";
}
#Validate Category is Chosen
#Validate Quantity on hand not less than 0
if ( $Quantity < 0 ) {
print "$Quantity Quantity on-hand cannot be less than 0";
}
单选按钮通常用于预定义选项。所以只需将它设置为默认选项,这样最终用户就必须将其更改为其他选项,否则使用默认值
假设您想坚持使用 CGI 作为一项技术(坦率地说,这在 2015 年是荒谬的)那么您的代码可以简化为这样的东西。
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
my @errors;
#Validate data is entered in first 2 input boxes
foreach my $param (qw[Item Name]) {
if ( ! defined param($param) ) {
push @errors, "$param Field Cannot be Blank";
}
}
#Validate the correct amount is entered
my $cost = param('Cost');
if ( $cost >= .50 && $cost <= 1000 ) {
push @errors, 'Cost must be between $.50 and 00';
}
my $price = param('Price')
if ( $price >= 1.00 && $price <= 2000 ) {
push @errors, 'Price must be between .00 and 00';
}
#Validate Quantity on hand not less than 0
if ( param('Quantity') < 0 ) {
push @errors, 'Quantity on-hand cannot be less than 0';
}
print header;
# You now need to send an HTML response to the user. If you have errors
# in @errors, then you need to send them a page which describes what they
# have got wrong. If @errors is empty, then you should send them a page
# containing whatever information they need next.
if (@errors) {
# create and send an error page
} else {
# create and send the next page
}
尝试编写我的第一个 Perl/CGI 脚本并将 运行 用于某些领域,我不太了解如何继续前进。创建了一个 HTML 页面,其中包含用户输入并发送到脚本的信息。如果用户不遵循指示,该脚本会在第二页上将信息输出回错误给用户。我的表单如下所示:
- 项目编号:用户输入一个数字
- 产品名称:
- 产品成本:
- 售价:
产品类别: - 单选按钮 1 - 单选按钮 2 - 单选按钮 3
手头数量(输入字段)
提交按钮
如果用户没有 select 单选按钮,我如何测试并发送错误 我想要 return 应该由脚本在第二页上自动生成的利润金额(价格 - 成本)。到目前为止,这是我的脚本(我使用记事本++作为编辑器)
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);
local ( $buffer, @pairs, $pair, $name, $value, %FORM );
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
}
else {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split( /&/, $buffer );
foreach $pair ( @pairs ) {
( $name, $value ) = split( /=/, $pair );
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex())/eg;
$FORM{$name} = $value;
}
#variables for input text fields
$Item = $FORM{Item};
$Name = $FORM{Name};
$Cost = $FORM{Cost};
$Price = $FORM{Price};
$Quantity = $FORM{Quantity};
#Radio button
$letter = $FORM{letter};
#Validate data is entered in first 2 input boxes
if ( $Item eq "" ) {
print "$Item Field Cannot be Blank";
}
if ( $Name eq "" ) {
print "$Name Field cannot be Blank";
}
#Validate the correct amount is entered
if ( $Cost >= .50 && $Cost <= 1000 ) {
print "$Cost Cost must be between $.50 and 00";
}
if ( $Price >= 1.00 && $Cost <= 2000 ) {
print "$Price Price must be between .00 and 00";
}
#Validate Category is Chosen
#Validate Quantity on hand not less than 0
if ( $Quantity < 0 ) {
print "$Quantity Quantity on-hand cannot be less than 0";
}
单选按钮通常用于预定义选项。所以只需将它设置为默认选项,这样最终用户就必须将其更改为其他选项,否则使用默认值
假设您想坚持使用 CGI 作为一项技术(坦率地说,这在 2015 年是荒谬的)那么您的代码可以简化为这样的东西。
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
my @errors;
#Validate data is entered in first 2 input boxes
foreach my $param (qw[Item Name]) {
if ( ! defined param($param) ) {
push @errors, "$param Field Cannot be Blank";
}
}
#Validate the correct amount is entered
my $cost = param('Cost');
if ( $cost >= .50 && $cost <= 1000 ) {
push @errors, 'Cost must be between $.50 and 00';
}
my $price = param('Price')
if ( $price >= 1.00 && $price <= 2000 ) {
push @errors, 'Price must be between .00 and 00';
}
#Validate Quantity on hand not less than 0
if ( param('Quantity') < 0 ) {
push @errors, 'Quantity on-hand cannot be less than 0';
}
print header;
# You now need to send an HTML response to the user. If you have errors
# in @errors, then you need to send them a page which describes what they
# have got wrong. If @errors is empty, then you should send them a page
# containing whatever information they need next.
if (@errors) {
# create and send an error page
} else {
# create and send the next page
}