[ back to toc ]

Server side validation

Date: 2002/02/27 10:53

Q:
I'm trying to create a cgi script that validates form data. I have a main
html page, validate.html, that asks for name, address, and zipcode of the
user. It's action is set to validate.cgi, where it checks to see if all
the fields are filled in correctly and returns the original form(along
with correct or incorrect data the user has inputed) and an extra message
saying whether the user has made a mistake or not.

here's my code:

<html>
<head>
<title>Validation Form</title></head>
<body bgcolor="sky blue">
<h1>Validate Form</h1>
<p>
<form action="validate.cgi" method="post">
<table>
<tr>
<td>Last Name:</td>
<td><input type="text" name="last" size="20" maxlength="20"></td>
<td>First Name:</td>
<td><input type="text" name="first" size="20" maxlength="20"></td>
</tr>
<tr>
<td>Street Address:</td>
<td colspan="3"><input type="text" name="street" size="50"
maxlength="50"></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" size="30" maxlength="30"></td>
<td>State:</td>
<td><input type="text" name="state" size="2" maxlength="2"></td>
</tr>
<tr>
<td>Zipcode:</td>
<td><input type="text" name="zip" size="5" maxlength="5"></td>
</tr>
<tr>
<td><input type="submit" name="enter" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

MAIN CGI:

#!/usr/local/bin/perl -w

use CGI;
$q = new CGI;

$inlast=$q->param('last');
$infirst=$q->param('first');
$instreet=$q->param('street');
$incity=$q->param('city');
$instate=$q->param('state');
$inzip=$q->param('zip');

$value = "";
$form1 = "validate.pl";
$form2 = "validateaccept.pl";

$message = " ";

if($inzip ne "")
{ $state = uc($state);
if($inzip =~ m/\D/)
{
$message = "\"$inzip\" is not a valid zipcode.\n";
$printForm = $form1;
}
else
{
if(length($inzip) != 5)
{
$message = "A zipcode must have 5 digits.\n";
$printForm = $form1;
}
else
{
$printForm = $form2;
}
}
}
else
{
$printForm = $form1;
}

print $q->header();
require $printForm;
print $q->end_html();

VALIDATE.PL:

#!/usr/local/bin/perl -w

print "<h3>$message</h3>";

print <<END;

<body bgcolor="sky blue">

<form action="validate.cgi" method="post">
<table>
<tr>
<td>Last Name:</td>
<td><input type="text" name="last" size="20" maxlength="20"></td>
<td>First Name:</td>
<td><input type="text" name="first" size="20" maxlength="20"></td>
</tr>
<tr>
<td>Street Address:</td>
<td colspan="3"><input type="text" name="street" size="50"
maxlength="50"></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" size="30" maxlength="30"></td>
<td>State:</td>
<td><input type="text" name="state" size="2" maxlength="2"></td>
</tr>
<tr>
<td>Zipcode:</td>
<td><input type="text" name="zip" size="5" maxlength="5"></td>
</tr>
<tr>
<td><input type="submit" name="enter" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

END

VALIDATEACCEPT.PL:
#!/usr/local/bin/perl -w

print <<END;

<body bgcolor="sky blue">
print "Your data has been successfully stored.\n";

<form action="validate.cgi" method="post">
<table>
<tr>
<td>Last Name:</td>
<td><input type="text" name="last" size="20" maxlength="20"></td>
<td>First Name:</td>
<td><input type="text" name="first" size="20" maxlength="20"></td>
</tr>
<tr>
<td>Street Address:</td>
<td colspan="3"><input type="text" name="street" size="50"
maxlength="50"></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" size="30" maxlength="30"></td>
<td>State:</td>
<td><input type="text" name="state" size="2" maxlength="2"></td>
</tr>
<tr>
<td>Zipcode:</td>
<td><input type="text" name="zip" size="5" maxlength="5"></td>
</tr>
<tr>
<td><input type="submit" name="enter" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

END

How come it's not working? when I press the sumit button on the original
html page, the page does go to validate.cgi but doesn't show anything.
Help!!

A:
I do not know for sure. However I can see that the action in the form is
validate.cgi, which means that the CGI script is supposed to be in the
same directory where the HTML file is. This is usually not the case.
Please check the relative URL of the cgi (relative to the HTML file).

Regards,
Peter

[ back to toc ]