如果用户未选择单选按钮Perl / CGI,如何发送错误消息 [英] How can I send error message if user doesn't select a radio button Perl/CGI

查看:95
本文介绍了如果用户未选择单选按钮Perl / CGI,如何发送错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Attempting to code my very first Perl/CGI script and have run into some areas I don't quite understand how to move forward. Created a HTML page that has information a user inputs and is sent to the script. The script then outputs the information on a second page back to the user with errors should the user not follow the directions. My forms take on an appearance like the following:


  • 项目编号:用户输入数字

  • 产品名称:

  • 产品成本:

  • 售价:

  • Item Number: User enters a number
  • Product Name:
  • Product Cost:
  • Selling Price:

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

Product Category: - radio button 1 - radio button 2 - radio button 3


  • 现有数量(输入字段)

  • quantity on hand (input field)

提交按钮

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

How do i test and send an error if user doesn't select a radio button I want to return the profit amount (price - cost) which should be auto generated on the second page by the script. Here is my script thus far (i'm using notepad++ as an editor)

#!/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($1))/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 $1000";
}

if ( $Price >= 1.00 && $Cost <= 2000 ) {
    print "$Price Price must be between $1.00 and $2000";
}

#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年是荒谬的),那么您的代码可以简化为类似的东西。

Assuming that you want to stick with CGI as a technology (which, frankly, is ridiculous in 2015) then your code can be simplified to something like this.

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

这篇关于如果用户未选择单选按钮Perl / CGI,如何发送错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆