2017-08-27 115 views
0

我正在爲我的網站構建一個註冊表單我使用jquery驗證了我的註冊頁面我想要這樣做當所有字段都填充有效的方式,然後註冊頁面重定向或加載和存儲在數據庫中的數據...如何檢查ajax使用jquery返回true或false值

1.第一jquery的代碼檢查輸入字段

2. jQuery代碼然後有一個AJAX調用,其檢查是否已經電子郵件存在與否如果ajax調用返回true,那麼數據將被插入到數據庫中,如果它返回false頁面將不會加載並顯示消息t帽子電子郵件已存在

在我的代碼中的問題是,當ajax調用是true或false我的頁面不斷加載並在數據庫中插入數據,但我希望如果值爲false頁面將不會加載。

這裏是我的代碼

$.ajax({ 
     type:'POST', 
     url:'email_validator.php', 
     data:{email:mail}, 
     success:function (response){ 
     var result = $.parseJSON(response); 
      if (result.status===false) { 
       $('#error').html("Email alreaday exist"); 
       return false; 
      } if(result.status===true) { 
       return true; 
      } 
     } 
    }); 

,這裏是我的email_validator.php

<?php 
     if(isset($_POST["email"])){ 
      $result = array(); 
      $email=$_POST["email"]; 
      $db=new PDO("mysql:host=localhost;dbname=the_scops","root",""); 
      $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $STH=$db->prepare("SELECT email FROM signup WHERE email=?"); 
      $STH->execute([$email]); 
      if($STH->rowCount() == 1){ 
       //echo "Email alreday exist"; 
       $result["status"] = false; 
      } 
      else{ 
       $result["status"] = true; 
      } 
       echo json_encode($result); 
      exit(0); 
     } 

回答

0

您可以添加異步選項設置爲false,並返回Ajax調用外:

function testAjax() { 
    var resultStatus = ""; 

    $.ajax({ 
     type:'POST', 
     url:'email_validator.php', 
     async: false, 
     data:{email:mail}, 
     success:function (response){ 
     var result = $.parseJSON(response); 
     resultStatus = result.status; 

     if (result.status===false) { 
      $('#error').html("Email alreaday exist"); 
     } 
     } 
    }); 

    return resultStatus; 
} 

[更新]

但是你不需要在客戶端檢查這一點。喲可以檢查是否在您的email_validator.php存在的電子郵件,並立即將其寫入數據庫,如果不存在的話:

email_validator.php

<?php 
     if(isset($_POST["email"])){ 
      $result = array(); 
      $email=$_POST["email"]; 
      $db=new PDO("mysql:host=localhost;dbname=the_scops","root",""); 
      $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $STH=$db->prepare("SELECT email FROM signup WHERE email=?"); 
      $STH->execute([$email]); 
      if($STH->rowCount() == 1){ 
       //echo "Email alreday exist"; 
       $result["status"] = false; 
      } 
      else{ 
       $result["status"] = true; 

       // --> INSERT the new email into the database 
      } 
       echo json_encode($result); 
      exit(0); 
     } 
0

試試這個:

$.ajax({ 
    type:'POST', 
    url:'email_validator.php', 
    data:{email:mail}, 
    success:function (response){ 
     if (response.status === true) { 
      return true; 
     } else { 
      $('#error').html("Email alreaday exist"); 
      return false; 
     } 
    } 
}); 
0

在我您應該調用PHP API方法直接在成功函數回調中插入新郵件:

$.ajax({ 
     type: 'POST', 
     url: 'email_validator.php', 
     data: {email: mail}, 
     success: function (response) { 
      var result = $.parseJSON(response); 

      if (result.status === false) { 
       $('#error').html("Email already exists"); 
      } 
      else (result.status === true) { 
       // -> Call here the PHP api method to insert the new email in the database 
      } 
     } 
    });