2016-02-12 54 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', 
    } 
}