0

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:

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

  • quantity on hand (input field)

  • SUBMIT Button

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";
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
allendks45
  • 339
  • 5
  • 18
  • Where did you get that code from? You're loading CGI.pm but not using it. You're using an old and potentially very broken CGI request parser. You're using `local` to define variables that should be defined with `my`. You're not using `strict` or `warnings`. The whole thing looks like code code from 1995. Please read some recent resources on Perl web programming. – Dave Cross Jul 10 '15 at 08:58

2 Answers2

1

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
}
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
0

Radio button are usually for predefined options. So just set it to a default option and that way the end user will have to change it to something else or else use the default

neemie
  • 83
  • 1
  • 7
  • Well, assuming that they use the original form to create the HTTP request. Which is something you should never assume. – Dave Cross Jul 10 '15 at 09:00
  • Found what appears to be a good site to learn perl/cgi. couple of questions that are not clearly identified on the site. If my html form is broken into sections (text box area, radio button, and then at bottom of page a submit button) should the data be passed using different scripts or can all form data be validated at one time? – allendks45 Jul 10 '15 at 21:25
  • Be careful. Most Perl/CGI tutorials on the web are terrible. It's worth checking the one you have found on the [Perl Tutorial Hub](http://perl-tutorial.org/). – Dave Cross Jul 11 '15 at 05:13
  • @Dave much appreciated. I was using CGI 101 as a guide plus a couple other sites not in the Hub directory. – allendks45 Jul 12 '15 at 16:14
  • The second edition of CGI 101 was a vast improvement on the first - but it was published over ten years ago. Things have moved on a lot since then. – Dave Cross Jul 12 '15 at 16:51