Send error page from HTML form when not filled out completely

I am working on a project where users will enter info into an HTML form and the information is stored to an HTML file for viewing later.  I need to shoot back an error if information is not filled out completely.  The catch is I need to send this info back in an HTML form, I can't use the die function.  My code is below if someone can have a look and steer me in the right direction.

#!/usr/bin/perl

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

my $q = new CGI;
my %p = $q->Vars;      #used to split the inputed information into usable variables
my $formdata = 'formdata.html'; #sets the value of my output file

TestInput();  #tests input to varify all fields are filled

open (MYFILE, ">>../service/$formdata") or die "Can not append to $formdata $!\n";  #opens my output file specifying path

Feedback();  #sends and HTML form the the user aknowledging input and echoing it to the user
Loginfo();   #sends the info to the formdata.html file for later viewing using a web browser

close MYFILE;

sub Feedback  #Feedback sub to echo info back to user
{
my $num = $p{'RESULT_TextField-9'};
(my $format_num = $num) =~ s/(\d{3})(\d{3})(\d{4})/($1)-$2-$3/;  #format the phone number in traditional style

print $q->header, $q->start_html('Confirmation');
print $q->h1('Thank you');
print GetTime();

print <<EOF;
Content-type: text/html

<P><FONT SIZE=4>
Your cooperation has been greatly appreciated.<br>
Now that we have your information, one of our hired goons<br>
will be paying you a visit soon.<br>
For your own edification, your information is below.<br>
Don't worry it has been securely entered into our mainframe<br>
with no chance of retrieval.<br>
<table border =0>
<tr><td><b>First Name:</b></td><td>$p{'RESULT_TextField-2'}</td></tr>
<tr><td><b>Last Name:</b></td><td> $p{'RESULT_TextField-3'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-4'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-5'}</td></tr>
<tr><td><b>City:</b></td><td> $p{'RESULT_TextField-6'}</td></tr>
<tr><td><b>State:</b></td><td> $p{'RESULT_TextField-7'}</td></tr>
<tr><td><b>Zip Code:</b></td><td> $p{'RESULT_TextField-8'}</td></tr>
<tr><td><b>Phone Number:</b></td><td> $format_num</td></tr>
EOF

}

sub Loginfo #Loginfo sub to send the info the outfile
{
print $q->start_html('Log');
my $num = $p{'RESULT_TextField-9'};
(my $format_num = $num) =~ s/(\d{3})(\d{3})(\d{4})/($1)-$2-$3/;
my $time = GetTime();
print MYFILE <<EOF;
<P><FONT SIZE=3>

<table border =0>
<tr><td>$time</td></tr>
<tr><td><b>First Name:</b></td><td>$p{'RESULT_TextField-2'}</td></tr>
<tr><td><b>Last Name:</b></td><td> $p{'RESULT_TextField-3'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-4'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-5'}</td></tr>
<tr><td><b>City:</b></td><td> $p{'RESULT_TextField-6'}</td></tr>
<tr><td><b>State:</b></td><td> $p{'RESULT_TextField-7'}</td></tr>
<tr><td><b>Zip Code:</b></td><td> $p{'RESULT_TextField-8'}</td></tr>
<tr><td><b>Phone Number:</b></td><td> $format_num</td></tr>
-------------------------------------------------------<br>
EOF
print $q->end_html;
}

sub TestInput # sub to test the input values and return appropriate errors
{
die "C'mon don't you know your own name?  Please go back and fix it\n" unless $p{'RESULT_TextField-2'};
die "C'mon don't you know your own name?  Please go back and fix it\n" unless $p{'RESULT_TextField-3'};
die "It's your address it isn't that hard, now let's try again\n" unless $p{'RESULT_TextField-4'};
die "Please enter your city\n" unless $p{'RESULT_TextField-6'};
die "You didn't enter a state, lets try again, it's just two letters\n" unless $p{'RESULT_TextField-7'};
die "five numbers, I didn't know it was that hard\n" unless $p{'RESULT_TextField-8'};
die "You gotta give me your digits.\n" unless $p{'RESULT_TextField-9'};
   
}

sub GetTime  #sub to change machine time to a readable format for timestamping
{
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
(my $second, my $minute, my $hour, my $dayOfMonth, my $month, my $yearOffset, my $dayOfWeek, my $dayOfYear, my $daylightSavings) = localtime();
my $year = 1900 + $yearOffset;
my $dateTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
return $dateTime;
}
urobinsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

urobinsAuthor Commented:
on another note, I have thought about creating a sub called TestHtml where I can just send the error code from test input....  so instead of die "XXXXXXXX" unless etc I could say TestHtml "XXXXXX" and it would print this.... What Im not sure how to do is this.  How do I tell my sub testHtml to print what ever it is sent as an argument... is there a way to do this?




sub TestHtml
{

print $q->start_html('Error');
print $q->h1('You have made a mistake');
print <<EOF;
<p><Font Size=3>
$errorCode
EOF
print $q->end_html;
FishMongerCommented:
TestHtml("C'mon don't you know your own name?  Please go back and fix it\n") unless $p{'RESULT_TextField-2'};

sub TestHtml
{
my $errorcode = shift;

print $q->start_html('Error');
print $q->h1('You have made a mistake');
print <<EOF;
<p><Font Size=3>
$errorCode
EOF
print $q->end_html;


Instead of your GetTime subroutine, here's a cleaner method for getting/formatting the date.

use POSIX qw(strftime);
print strftime("\n%H:%M:%S, %a %b %d, %Y", localtime);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
urobinsAuthor Commented:
I am getting an internal software error if I run this (the only way this seems to go away is to add $q-header to the beginning of the code...
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

urobinsAuthor Commented:
Thank you very much, the shift works great, the problem I have is I need to add $q->header in order for the program to launch (otherwise I get internal server error)  but if I add that then I get this spit out

Content-Type: text/html; charset=ISO-8859-1

at the end of my repsonse is there anything I can do for that?


and thanks for the time help, I didn't know that existed when I started this and took the time to write that lengthy code :)
ahoffmannCommented:
$q->header needs to be printed *before* any other output
urobinsAuthor Commented:
I figured out what was going on with that.  I need that before any paged output and if it appears more than once it throws that response.. I do have a question here though

I would like to return all the errors on one page of output and was trying to create one long string to do so, is there a way to concantenate these values in execution I tried += and it doesn't seem to recognize as it skips the error condition and goes right to the confirmation.  I have included all code incase you see a conflict or something.


#!/usr/bin/perl

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $flag =0;

my $q = new CGI;
my %p = $q->Vars;      #used to split the inputed information into usable variables
my $formdata = 'formdata.html'; #sets the value of my output file



TestInput();  #tests input to varify all fields are filled

open (MYFILE, ">>../service/$formdata") or die "Can not append to $formdata $!\n";  #opens my output file specifying path
if ($flag==0)
{
    Feedback();  #sends and HTML form the the user aknowledging input and echoing it to the user
    Loginfo();   #sends the info to the formdata.html file for later viewing using a web browser
}
else
{
    END
}

close MYFILE;

sub Feedback  #Feedback sub to echo info back to user
{
my $num = $p{'RESULT_TextField-9'};
(my $format_num = $num) =~ s/(\d{3})(\d{3})(\d{4})/($1)-$2-$3/;  #format the phone number in traditional style

print $q->header,$q->start_html('Confirmation');
print $q->h1('Thank you');
print GetTime();

print <<EOF;


<P><FONT SIZE=4>
Your cooperation has been greatly appreciated.<br>
Now that we have your information, one of our hired goons<br>
will be paying you a visit soon.<br>
For your own edification, your information is below.<br>
Don't worry it has been securely entered into our mainframe<br>
with no chance of retrieval.<br>
<table border =0>
<tr><td><b>First Name:</b></td><td>$p{'RESULT_TextField-2'}</td></tr>
<tr><td><b>Last Name:</b></td><td> $p{'RESULT_TextField-3'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-4'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-5'}</td></tr>
<tr><td><b>City:</b></td><td> $p{'RESULT_TextField-6'}</td></tr>
<tr><td><b>State:</b></td><td> $p{'RESULT_TextField-7'}</td></tr>
<tr><td><b>Zip Code:</b></td><td> $p{'RESULT_TextField-8'}</td></tr>
<tr><td><b>Phone Number:</b></td><td> $format_num</td></tr>
EOF
print $q->end_html;
}

sub Loginfo #Loginfo sub to send the info the outfile
{
print $q->start_html('Log');
my $num = $p{'RESULT_TextField-9'};
(my $format_num = $num) =~ s/(\d{3})(\d{3})(\d{4})/($1)-$2-$3/;
my $time = GetTime();
print MYFILE <<EOF;
<P><FONT SIZE=3>

<table border =0>
<tr><td>$time</td></tr>
<tr><td><b>First Name:</b></td><td>$p{'RESULT_TextField-2'}</td></tr>
<tr><td><b>Last Name:</b></td><td> $p{'RESULT_TextField-3'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-4'}</td></tr>
<tr><td><b>Street Address:</b></td><td> $p{'RESULT_TextField-5'}</td></tr>
<tr><td><b>City:</b></td><td> $p{'RESULT_TextField-6'}</td></tr>
<tr><td><b>State:</b></td><td> $p{'RESULT_TextField-7'}</td></tr>
<tr><td><b>Zip Code:</b></td><td> $p{'RESULT_TextField-8'}</td></tr>
<tr><td><b>Phone Number:</b></td><td> $format_num</td></tr>
-------------------------------------------------------<br>
EOF
print $q->end_html;
}

sub TestInput # sub to test the input values and return appropriate errors
{
my$ErrorString="";
$flag =1 && $ErrorString +="C'mon don't you know your own name?  Please go back and fix it\n" unless $p{'RESULT_TextField-2'};
#$flag =1 && ErrorHtml ("C'mon don't you know your own name?  Please go back and fix it\n") unless $p{'RESULT_TextField-3'};
#$flag =1 && ErrorHtml ("It's your address it isn't that hard, now let's try again\n") unless $p{'RESULT_TextField-4'};
#$flag =1 && ErrorHtml ("Please enter your city\n") unless $p{'RESULT_TextField-6'};
#$flag =1 && ErrorHtml ("You didn't enter a state, lets try again, it's just two letters\n") unless $p{'RESULT_TextField-7'};
#$flag =1 && ErrorHtml ("five numbers, I didn't know it was that hard\n") unless $p{'RESULT_TextField-8'};
#$flag =1 && ErrorHtml ("You gotta give me your digits.\n") unless $p{'RESULT_TextField-9'};
if ($flag==1)
{
    ErrorHtml($ErrorString);
}

}

sub GetTime  #sub to change machine time to a readable format for timestamping
{
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
(my $second, my $minute, my $hour, my $dayOfMonth, my $month, my $yearOffset, my $dayOfWeek, my $dayOfYear, my $daylightSavings) = localtime();
my $year = 1900 + $yearOffset;
my $dateTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
return $dateTime;
}

sub ErrorHtml
{

my $errorCode = shift;

print $q->header,$q->start_html('Error');
print $q->h1('You have made a mistake');
print <<EOF;
<p><Font Size=3>
$errorCode
EOF
print $q->end_html;
}

Adam314Commented:
You can concatenate two string with the . operator, or put them both in double-quotes:

$string1="This is string 1";
$string2="This is string 2";

$both1 = $string1 . $string2;
$both2 = "$string1$string2";

$both1 and $both2 will be the same
urobinsAuthor Commented:
Okay, I will give that a shot thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.