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";
}