如果用户未选择单选按钮Perl / CGI,如何发送错误消息

时间:2015-07-09 23:14:46

标签: html5 perl cgi

尝试编写我的第一个Perl / CGI脚本并遇到某些方面,我不太明白如何继续前进。创建了一个HTML页面,其中包含用户输入的信息并将其发送到脚本。然后,如果用户不遵循指示,则脚本然后将第二页上的信息输出回用户而有错误。我的表格呈现如下:

  • 项目编号:用户输入数字
  • 产品名称:
  • 产品成本:
  • 售价:

产品类别: - 单选按钮1 - 单选按钮2 - 单选按钮3

  • 现有数量(输入字段)

  • 提交按钮

如果用户没有选择单选按钮,我该如何测试并发送错误 我想返回脚本应在第二页上自动生成的利润金额(价格 - 成本)。这是我到目前为止的脚本(我使用notepad ++作为编辑器)

    int FTimeND;
    FTimeND = FACTime[2];

    float FTimeDec;
    FTimeDec = FACTime[2]-FTimeND;

    int FTime;
    FTime = FTimeDec*60;

     cout <<FTimeND<<" hours and "<<FTime<< " minutes\n";

2 个答案:

答案 0 :(得分:1)

假设您希望坚持使用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 $1000';
}

my $price = param('Price')
if ( $price >= 1.00 && $price <= 2000 ) {
  push @errors, 'Price must be between $1.00 and $2000';
}

#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
}

答案 1 :(得分:0)

单选按钮通常用于预定义选项。所以只需将其设置为默认选项,最终用户必须将其更改为其他内容,否则使用默认选项