if / else语句在PHP和Ajax中输入验证

时间:2012-03-01 19:16:45

标签: php html ajax validation

我正在通过Ajax将一些数据发送到处理注册的PHP脚本。我正在尝试从服务器获得输入是否有效的响应(实时验证)。我希望每个输入都有自己的响应,这意味着如果第一个输入仍然无效并且有人在第二个输入字段中放置了一些无效的内容,则第一个响应会保留,并且它还会发送第二个响应。

现在发生的是:第一个输入是无效的,我看到了响应,但是当我进入下一个输入字段并在字段中放入一些无效的东西时,第一个响应就停留在那里。(我已经用控制台对此进行了测试。登录Chrome)

更新:举个例子,我看到了这个:not a valid username!,然后我在下一个字段中放了一些无​​效的电子邮件地址,我仍然看到not a valid username!

这是我的PHP代码:

if(isset($_POST['username'])  && isset($_POST['email']) && isset($_POST['email2']) && isset($_POST['password']) 
&& isset($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['gender']) && isset($_POST['day'])
&& isset($_POST['month'])  && isset($_POST['year']) ) {

    $username = $_POST['username'];
    $email = $_POST['email'];
    $email2 = $_POST['email2'];
    $password = $_POST['password'];
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $gender = $_POST['gender'];
    $day = $_POST['day'];
    $month = $_POST['month'];
    $year = $_POST['year'];


    if(!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i",$username)){

        echo "not a valid username.";
    }
    else if(filter_var($email,FILTER_VALIDATE_EMAIL)){

        echo "OK!";
    }
    else if(!filter_var($email,FILTER_VALIDATE_EMAIL)){

        echo "not a valid email address";
    }
    else if(strcmp($email,$email2) != 0){

        echo "emails are different.";
    }
    else if(strcmp($email,$email2) == 0){

        echo "OK!";
    }
    else if(!preg_match("[a-zA-Z]*",$firstname)){

        echo "Not a valid firstname.";

    }
    else if(preg_match("[a-zA-Z]*",$firstname)){

        echo "OK!";

    }
    else if(!preg_match("[a-zA-Z]*",$surname)){

        echo "not a valid surname.";

    }
    else if(preg_match("[a-zA-Z]*",$surname)){

        echo "OK!";
    }
}

这是JQuery Ajax代码:

function handlePost() {  

 var username = $('#username').val();  
 var email = $('#email').val();  
 var email2 = $('#email2').val(); 
 var password = $('#password').val();   
 var firstname = $('#firstname').val();
 var surname = $('#surname').val();
 var gender = $('#gender').val();
 var day = $('#day').val();
 var month = convertMonth($('#month').val())
 var year = $('#year').val();

 $.ajax({  
   type: "POST",  
   url: "handleRegister.php",  
   data: "username="+username+"&email="+email+"&email2="+email2+"&password="+password+"&firstname="
   +firstname+"&surname="+surname+"&gender="+gender+"&day="+day+"&month="+month+"&year="+year,  
   success: function(resp){  
     // we have the response  
     //alert("Server said:\n '" + resp + "'");  

     console.log("Server said:\n '" + resp + "'")
   },  
   error: function(e){  
     //alert('Error: ' + e);  
     console.log("Server said:\n '" + e + "'")
   }  
 });  
} 

我认为这是我使用if / else的方式。另外我对如何/何时使用isset($ _ POST ['submit'])感到有点困惑?

提前致谢。

3 个答案:

答案 0 :(得分:2)

将部分else if更改为if。仅使用else if,一旦条件与其余条件匹配,就会跳过。因此,当您的用户名无效时,您的代码永远不会检查电子邮件是否有效。

仅在需要一个输出时才使用else if

答案 1 :(得分:1)

你想要做的是,而不是别的,只是喜欢:

 if(!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i",$username)){

        echo "not a valid username.";
    }
 if(filter_var($email,FILTER_VALIDATE_EMAIL)){

        echo "OK!";
 }
 if(!filter_var($email,FILTER_VALIDATE_EMAIL)){

    echo "not a valid email address";
 }
 if(strcmp($email,$email2) != 0){

     echo "emails are different.";
 }
 etc....

答案 2 :(得分:1)

$errors = array();

if (!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i", $username))
{
    $errors[] = "not a valid username.";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    $errors[] = "not a valid email address";
}
if ($email !== $email2)
{
    $errors[] = "emails are different";
}
if (!preg_match("[a-zA-Z]*", $firstname))
{
    $errors[] = "Not a valid firstname.";
}
if (!preg_match("[a-zA-Z]*", $surname))
{
    $errors[] = "not a valid surname.";
}

if ($errors)
{
    echo implode("\n", $errors);
}
else
{
    echo 'OK!';
}