Im having major problems with the validation code below. Basically it is checking a simple form for errors. If errors are found it saves them in the array $errors. It all worked fine checking for and displaying the errors. the problem is when there are no errors! checks using the code:
if (count($errors) == 0) {
header ("location: page2.php");
exit;
}
and should pass to the next page but it just gives me a blank on the same page. Someone help me please!
The full code is below:
<?php
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email)){
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
return false;
}
return true;
}
return false;
}
$submitted = $_POST['submitted'];
?>
<?php
if ($submitted != 0) {
$name = trim($_POST['name']);
if (strlen($name)<1) {
$errors['name'] = "please enter your name";
}
$email2 = trim($_POST['email']);
if(!checkEmail($email)) {
$errors['email'] = "please enter a valid email address";
}
$email = trim($_POST['email2']);
if ($email != $email2) {
$errors['email2'] = "email addresses do not match";
}
$comments = ($_POST['comments']);
if (strlen($comments)<30) {
$errors['comments'] = "please give more details";
}
if (count($errors) == 0) {
header ("location: target.php");
exit;
}
}
?>
</head>
<body>
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>name:
<input name="name" type="text" id="name" />
<?php echo $errors['name']; ?></p>
</p>
<p>email:
<input type="text" name="email" />
<?php echo $errors['email']; ?></p>
<p>enter your email again:
<input name="email2" type="text" id="email2" />
<?php echo $errors['email2']; ?></p>
<p>comments:</p>
<p>
<textarea name="comments" id="comments"></textarea>
<?php echo $errors['comments']; ?>
</p>
<p>
<input name="submitted" type="hidden" id="submitted" value="1" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
<p> </p>
</form>
Thankyou