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 工作?

4

2 回答 2

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();
  });
});
于 2013-01-10T17:27:12.770 回答
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;
    });
});
于 2013-01-10T17:24:18.213 回答