Page 1 of 1

Validation Problem

Posted: Fri Mar 18, 2005 4:23 am
by bailo81
I have written a simple validation script but cant get it to pass to the next page if there are no errors. All the validation works okay but it just gives me a blank page if there are no errors??? The code is below:

Code: Select all

<?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";
		}
		
}
?>

</head>

<body>
<?php 		if (count($errors) == 0) {
		header ("location: target.php");
		
		exit;
		}
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <p>name: 
    <input name="name" type="text" id="name" />
		    &nbsp;
  <?php echo $errors['name']; ?></p>
  </p>
  <p>email:
    <input type="text" name="email" /> 
    &nbsp;
     <?php echo $errors['email']; ?></p>
  <p>enter your email again: 
    <input name="email2" type="text" id="email2" />
	    &nbsp;
  <?php echo $errors['email2']; ?></p>
  <p>comments:</p>
  <p>
    <textarea name="comments" id="comments"></textarea>
		    &nbsp;
  <?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>&nbsp;    </p>
</form>
cheers


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Mar 18, 2005 5:57 am
by n00b Saibot
since you are not redirecting anywhere nor you are outputing any confirmation after validation. thats why you are getting blank page. page is working fine.

Posted: Fri Mar 18, 2005 8:11 am
by s.dot
Your code is a bit messed up with having code in the <head> section and then code in the body section.. when it doesn't seem necessary to do so.

But that shouldn't be the problem. Use a redirect before any output HTML like:

Code: Select all

header("Location: page.php?var=value");
or provide a comfirmation that their validation has been accepted, and perhaps provide a link for them to click on to continue.