• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

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;
}
0
urobins
Asked:
urobins
2 Solutions
 
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;
0
 
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);
0
 
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...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 :)
0
 
ahoffmannCommented:
$q->header needs to be printed *before* any other output
0
 
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;
}

0
 
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
0
 
urobinsAuthor Commented:
Okay, I will give that a shot thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now