2013-01-10 70 views
0

我做了一個小的jQuery Ajax代碼檢查電子郵件的數據庫已經存在,我不能有工作的權利 我的HTML代碼:檢查形式與阿賈克斯

<form name="emailForm" id="emailForm" method="post" action="signin.php"> 
    <input type="email" name="email" id="email" required="required" /> 
    <input type="submit" value="Get Started Now!" /> 
</form> 

jQuery代碼:

$(function(){ 
    $("#emailForm").submit(function(){ 
     $.ajax({ 
      type:POST, 
      url:"signin.php", 
      data: "email="+$("#email").val(),//{email:$("#email").val()}, 
      success: function(msg){ 
       if(msg == '1'){ 
        alert("Already exists."); 
       }else{ 
        alert("C'est cool, Hak la suite"); 
       } 
      } 

     }); 
     return false; 
    }); 
}); 

的signin.php代碼

function check_email($email){ 
    include 'bdd.php'; 
    $query = "SELECT * FROM users WHERE email = '$email'"; 
    $resultat = mysqli_query($link,$query); 
    $msg = mysqli_num_rows($resultat); 
    return $msg; 
} 
if(isset($_POST['email'])){ 
    if(check_email($_POST['email']) == 1){ 
     $msg = "1"; 
    }else{ 
     $msg = "0"; 
    } 
} 
echo $msg; 

它前進到signin.php並打印1或0,任何想法豪讓Ajax工作?

+0

是它打印1或0,在 – user1548996

+0

signin.php頁這是不正確的從PHP返回數據到jquery的方式? – user1548996

+0

POST應該使用單引號或雙引號。 –

回答

0

你嘗試用下面的代碼

$(function(){ 
    $("#emailForm").submit(function(){ 
     $.ajax({ 
      type:POST, 
      url:"signin.php", 
      data: {"email":$("#email").val()}, 
      success: function(msg){ 
       if(msg == '1'){ 
        alert("Already exists."); 
       }else{ 
        alert("C'est cool, Hak la suite"); 
       } 
      } 

     }); 
     return false; 
    }); 
}); 
+0

同樣的事情,它在signin.php中迴應0或1 – user1548996

0

試試這個,看看是否這會有所幫助:

$(function() { 
    $("#emailForm").submit(function (e) { 
    $.ajax({ 
     type: 'POST', // in quotes 
     url: "signin.php", 
     data: {data:$(this).serialize()}, // serialize the form 
     success: function (msg) { 
     if (msg == '1') { 
      alert("Already exists."); 
     } else { 
      alert("C'est cool, Hak la suite"); 
     } 
     } 
    }); 
    e.preventDefault(); 
    }); 
});