something wrong with my php function?

Bleh22

New member
my php function validateForm() always returns $result as 3, and prints out all of the three errors, even if there was only one error or no error at all. I don't know why. Even if I re-open the page, same thing.

<?php

function printForm() {
echo("<form method='post' action='testerror.php'>
Username: <input type='text' name='username' size='20' /><br /><br />
First Name: <input type='text' name='firstname' size='20' />
Last Name: <input type='text' name='lastname' size='20' />");
}

function validateForm() {
$result = 0;
$error = array();
if(!preg_match("/^[a-zA-Z]+\D$/",$uname))
{
$errors[0] = "Please enter a user name that consists of only letters.";
}
if(!preg_match("/^[a-zA-Z]+\D$/",$fname))
{
$errors[1] = "Please enter a first name that consists of only letters.";
}
if(!preg_match("/^[a-zA-Z]+\D$/",$lname))
{
$errors[2] = "Please enter last name the consists of only letters.";
}
echo "Inside Validate Form. <br />";
$result = count($errors);
echo "From Valide Form result, $result <br />";
for($i=0; $i<$result; $i++) {

echo "For LOOP From Validate Form The number of errors is, $result. <br />";
echo "For LOOP From Validate echo errors, {$errors[$i]}. <br />";
print_r($errors[$i]);
}
}


if(isset($_REQUEST['firstname'])) {
$fname = $_REQUEST['firstname'];
$lname = $_REQUEST['lastname'];
$uname = $_REQUEST['username'];

echo"Inside If request. <br />";
validateForm();
}
else {
printForm();
}
?>
the last parenthesis are not missing, for some reason yahoo answers decided to skip them out.
 
Back
Top