1
<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Amateur</title>
    <link rel="stylesheet" href="css/reset.css" type="text/css">
    <script src="http://code.jquery.com/jquery-1.8.0.js">
        function check_email()
        {
            var email=$("#txtEmail").val();
            $.ajax(
            {
                type:"POST";
                url:"index.php";
                data:"email="+email,
                success:function(msg)
                {
                    $("#chkEmail").html(msg);
                }
            });

            return false;
        }
    </script>
</head>

<body>
<form method="post">
    <label for="txtEmail">E-mail:</label>
        <input id="txtEmail" name="email" type="email" onblur="return check_email()">
    <label id="chkEmail" for="txtEmail"></label>
    <?php

    if(isset($_POST['email']))
    {
        $user='root';

        $pdo=new PDO('mysql:host=localhost;dbname=class;charset=utf8',$user);

        $email=$_POST['email'];

        $stmt=$pdo->prepare('SELECT email from tbl_users WHERE email=:email LIMIT 1');
        $stmt->execute(array(':email'=>$email));

        if($stmt->rowCount()>0)
        {
            echo 'E-mail already use.';
        }
        else
        {
            echo 'E-mail not use.';
        }

    }

    ?>
</form>
</body>
</html>

我仍然是 PHP 和 JQuery 的初学者 我想知道如何解决这种类型的错误?我从萤火虫检查它。流程是,在用户输入完电子邮件后,它将自动从数据库中检查它是否存在。并且预期的输出不会显示在我的页面中。

4

6 回答 6

2

您缺少选择器上的引号,用分号而不是逗号等分隔 ajax 函数中的参数。

    function check_email() {
        var email=$("#txtEmail").val();
            $.ajax({
                type:"POST",
                url:"index.php",
                data: {email: email},
                success:function(msg) {
                    $("#chkEmail").html(msg);
                }
            });
            return false;
       }
于 2012-08-29T17:08:11.230 回答
1

添加另一个<script>标签,添加js源文件和相同标签中的代码不是正确的方法。

<script src="http://code.jquery.com/jquery-1.8.0.js"></script>
<script>
        function check_email()
        {
            var email=$("#txtEmail").val();
            $.ajax(
            {
                type:"POST";
                url:"index.php";
                data:"email="+email,
                success:function(msg)
                {
                    $("#chkEmail").html(msg);
                }
            });

            return false;
        }
    </script>
于 2012-08-29T17:35:16.400 回答
0

将您的 jquery 源代码放在单独的脚本上。确保为脚本放置一个结束标记(不是自结束标记)。

使用逗号作为参数,而不是分号。

尝试这个:

<script src="http://code.jquery.com/jquery-1.8.0.js"></script>
<script type="text/javascript">
    function check_email()
    {
        var email=$("#txtEmail").val();
        $.ajax(
        {
            type:"POST",
            url:"index.php",
            data:"email="+email,
            success:function(msg)
            {
                $("#chkEmail").html(msg);
            }
        });

        return false;
    }
</script>
于 2012-08-30T03:39:13.863 回答
0
<script src="http://code.jquery.com/jquery-1.8.0.js">
    function check_email()
    {
        var email=$("#txtEmail").val();
        $.ajax(
        {
            type:"post",
            url:"index.php",
            data:"email="+email,
            success:function(msg)
            {
                $("#chkEmail").html(msg);
            }
        });

        return false;
    }
</script>
于 2012-08-29T17:08:25.237 回答
0

编辑

$.ajax(
        {
            type:"post";
            url:"index.php";
            data:"email="+email,
    success:function(msg)
                    {
                        $(#chkEmail).html(msg);
                    }
             **TO**
$.ajax(
        {
            type:"POST",
            url:"index.php",
            data:{email:email},
success:function(msg)
                {
                    $('#chkEmail').html(msg);
                }
于 2012-08-29T17:08:31.150 回答
0
    <script src="http://code.jquery.com/jquery-1.8.0.js"></script>
    <script>
      var checkEmail= function()
        {
            var email=$("#txtEmail").val();
            $.ajax(
            {
                type:"POST",
                url:"index.php",
                data:"email="+email,
                success:function(msg)
                {
                    $("#chkEmail").html(msg);
                }
            });

            return false;
        }
    </script>
</head>

<body>
<form method="post">
    <label for="txtEmail">E-mail:</label>
        <input id="txtEmail" type="email" onblur="javascript:checkEmail();">
    <label id="chkEmail" for="txtEmail"></label>

</form>
</body>

添加到onblur="javascript:checkEmail();,我做了一个有点不同的函数,将它声明为 var。

于 2012-08-29T17:56:52.273 回答