2016-02-12 66 views
0

我正在使用jQuery验证远程来检查一个php文件,并查看一个电子邮件地址是否已被使用。它工作正常,但是,我需要它重定向到一个新的页面,如果输入的电子邮件已存在或“false”。以下是正在运行的远程验证脚本和简单的php文件。再次,这工作正常,只是不知道如何重定向到一个新的页面,如果PHP脚本返回false。jQuery验证假的远程重定向页面

 rules: { 
      fullname: "required", 
      email: { 
       required: true, 
       email: true, 
       remote: { 
        url: "validate-email.php", 
        type: "post", 
       }, 
      }, 

    messages: { 
    email: { 
     required: 'Please enter a valid email address', 
     remote: 'Sorry, this email address is already in use', 
     } 
    } 

验证,email.php

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); 
$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1"; 
$stmt = $mysqli->prepare($prep_stmt); 

if ($stmt) { 
    $stmt->bind_param('s', $email); 
    $stmt->execute(); 
    $stmt->store_result(); 

    if ($stmt->num_rows == 1) { 
     // A user with this email address already exists 
     echo 'false'; 
    }else{ 
     echo 'true'; 
    } 
} 

就更不用说了最后一次,上述PHP和jQuery脚本做工精细。如果php的结果是echo'false',只需要重定向到一个新页面;

+0

重定向也许[此](http://stackoverflow.com/questions/11342716/jquery-validation-form-remote-rule-success - 消息)将有所帮助?这应该允许您检索服务器响应。实际上,您需要添加一个父项:'success:function(e){}'。 ** e **变量是服务器的响应(即:“true”或“false”)。在这里,你可以做'if(e ==“true”){}' – TheMintyMate

+1

我不明白这一点。验证插件的全部要点是*停止*表单的提交如果/当它无效时。如果您向'remote'方法回送'false',那么这是一个“无效”形式,并且不应在用户修复其输入值之前提交。因此,既然你不关心用户修改他们的数据并提交表单,而不是回应'false',为什么不使用PHP重定向到一个新页面呢? – Sparky

+0

感谢TheMintyMate - 我能够做到这一点,只需添加成功即可:function(e){if(e == false){window.location.replace(“http://mywebsite.com”);}} –

回答

0

我能够通过简单地从TheMintyMate

添加信息
rules: { 
     fullname: "required", 
     email: { 
      required: true, 
      email: true, 
      remote: { 
       url: "validate-email.php", 
       type: "post", 
       success: function(e) { if(e == false){window.location.replace("http://website");}} 
      }, 
     }, 

messages: { 
email: { 
    required: 'Please enter a valid email address', 
    remote: 'Sorry, this email address is already in use', 
    } 
}