views:

39

answers:

2

I am wondering if anyone out there can help with my form Validation Please?

I am having a few problems trying to synchronized out how certain bits of the actual structure of the script works together.

<?php
$flag="OK";   // This is the flag and we set it to OK
$msg="";        // Initializing the message to hold the error messages
   if(isset($_POST['Send'])){
      $key=substr($_SESSION['key'],0,4);
      $num_key = $_POST['num_key'];
      if($key!=num_key){
      $msg=$msg."Your Key not valid! Please try again!<BR>";
      $flag="NOTOK";
           }
      else{
    $msg=$msg."Your Key is valid!<BR>";
    $flag="OK";
        } 
         }
$email=$_POST['email'];
echo "Your Email: ".$email." is";
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$msg=$msg."Invalid email<BR>";
$flag="NOTOK"; 
}else{
$msg=$msg."Valid Email<BR>";
$flag="OK";
}
$password=$_POST['password'];
if(strlen($password) < 5 ){ 
$msg=$msg."( Please enter password of more than 5 character length  )<BR>";
$flag="NOTOK"; 
}
if($flag <>"OK"){
echo "$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'>";
}else{ // all entries are correct and let us proceed with the database checking etc …
} 
function spamcheck($field)
  {
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
if (isset($_POST['email']))
  {//if "email" is filled out, proceed
  $mailcheck = spamcheck($_POST['email']); 
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
      }
?>

the problem, when email valid, password valid, though key is invalid the warning of key disappear, it mean passed too... and also the spamcheck doesn't look work..

+1  A: 

Each of you tests sets flag to "OK" or "NOTOK" overwriting decisions made by previous tests.
You could start with $flag = true;. And only if a test decides that the input is unsatisfying it sets $flag=false.
Or you can remove $flag altogether and check if 0===strlen($msg) after the tests.

VolkerK
I much appreciated your suggestions and thank you... by following your concept above, that avoid warning message of key and also spamcheck
jones
+1  A: 
Eineki
Thank for Your nice input so far Eineki, Let say you typed 4 block code.this is my conclusion by following your suggestion above:Used the 2nd block code, the results: in key validation invalid warning set as default.Used the 3rd block code, the result: passed all invalidate field and also error "}" in function ValidateKey() { if(!isset($_POST['Send'])) return true; } <--- error.
jones
Just removed another bug: $key==num_key in the key validation should be $key==$num_key.For the 3rd block opened the curly brace if(!isset($_POST['Send'])) { return true; }I removed the <?php and ?> tags: seems I've found a bug in the renderer of posts
Eineki
jones
@jones: php is a server side language, you have to post data in order to manipulate it (or check if they are valid). For validating the form field before sending the data you have to resort to a javascript library , for example http://livevalidation.com/
Eineki