# if the HTML has not yet been displayed, display it
if ($formdata{'mode'} eq "setup") {
open (HTML, "<../add_whole_meal.html") || die &show_error("Unable to open add_meal.html: $!");
unless ($content_type == 1) {
print "Content-type: text/html\n\n";
$content_type = 1;
}
while() {
$_ =~ s/\$(\w+)/$formdata{$1}/eg;
$_ =~ s/\+_errors_\+//;
print;
}
close(HTML);
}
elsif ($formdata{'mode'} eq "check") {
# first make sure there is at least one item that has been entered
if (($formdata{'name1'} eq "") && ($formdata{'name2'} eq "") && ($formdata{'name3'} eq "") && ($formdata{'name4'} eq "") && ($formdata{'name5'} eq "")) {
$error_message = $error_message . "
You must enter at least one item ";
$flag = 1;
}
# check to see if a name was given, that all other sections are filled in
if (($formdata{'name1'} ne "") && (($formdata{'ingredients1'} eq "") || ($formdata{'instructions1'} eq ""))) {
$error_message = $error_message . "Please be sure all required parts of item1 are filled in";
$flag = 1;
}
# removed some of the repetitive code
# check to make sure all numbers are actually numbers
if (($formdata{'calories'} !~ m/\d*/) || ($formdata{'calories'} !~ m/\d*/) || ($formdata{'calories'} !~ m/\d*/) || ($formdata{'calories'} !~ m/\d*/)) {
$error_message = $error_message . "Please be sure all of the general information is in numeric form";
$flag = 1;
}
# display error if needed and regenerate form
if ($flag == 1) {
open (HTML, "<../add_whole_meal.html") || die &show_error("Unable to open add_meal.html: $!");
unless ($content_type == 1) {
print "Content-type: text/html\n\n";
$content_type = 1;
}
# setup the error message
$error_message = "Unfortunately there was an error!";
# replace as needed and then reprint HTML
while() {
$_ =~ s/\$(\w+)/$formdata{$1}/eg;
$_ =~ s/\+_errors_\+/$error_message/;
print;
}
close(HTML);
}
}