2013-01-10 67 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(); 
    }); 
});