Skip to content Skip to sidebar Skip to footer

How Can I Send Error Message If User Doesn't Select A Radio Button Perl/CGI

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

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

Solution 2:

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


Post a Comment for "How Can I Send Error Message If User Doesn't Select A Radio Button Perl/CGI"