Question : Printing with cgi and Perl

I'm using several cgi scripts and using perl for the cgi pages, In one section which creates a user account, I have a section that checks to make sure that the new record contains a country (c), mail address (mail) and a manager (manager). If one of these fields was not entered then it prints a page giving the user the info they did not put in.

What I have so far works fine, that is if they only left one of the 3 fields blank, the problem I have is how to make it print out the items if they left 2 or 3 of the fields blank.

If they happen to leave all three fields blank I need it to print out a "bullet list" of the three items.

Currently if they leave one of the fields blank it will print out this.

Example of what is printing now if they leave the mail field blank.


It appears that some information has not been entered correctly.

The items needing attention are:

  • Need to enter a valid email address

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
if (($in{c} eq '') || ($in{mail} eq '') || ($in{manager} eq '')) {
  $err1 = "It appears that some information has not been entered correctly.";
  $err2 = "The items needing attention are:";

 if ($in{mail} eq '') {$missing = "Need to enter a valid \"email address\"";}
 if ($in{manager} eq '') {$missing = "Need to enter a valid \"Manager\" (manager)";}
 if ($in{c} eq '') {$missing = "Need to enter a valid \"Country\" (c) and \"City\" (l)";}

print <<"ERROR";
<html><head>
<title>Account creation Error</title>
</head><body>
$Header
<center>
<h2>Account creation Error</h2>
</center>
<td>$err1</td><br><br>
<td>$err2</td><br><br>
<li type="disc">$missing</li>
<p>
$Footer
ERROR
exit;
}

Answer : Printing with cgi and Perl

Hi bt707,
First of all, I would suggest that you don't bother with creating the HTML tags yourself but instead use the very standard CGI.pm (http://perldoc.perl.org/CGI.html). It will guarantee that you always produce well-formed HTML.

Re: your "missing" errors, what you need is collect all the errors in one array:
if ($in{mail} eq '') {push(@missing,"Need to enter a valid \"email address\"")}
 if ($in{manager} eq '') {push(@missing, "Need to enter a valid \"Manager\" (manager)")}
 if ($in{c} eq '') {push(@missing, "Need to enter a valid \"Country\" (c) and \"City\" (l)")}
And then print them all using something like this:

my $errorHtml = join("", map{"<li type="disc">$_</li>"} @missing);

and replace your <li type="disc">$missing</li>
with $errorHtml

Good luck!





Random Solutions  
 
programming4us programming4us