简体   繁体   English

我该如何解决这类错误:“未捕获的ReferenceError:check_email未定义onblur”

[英]How can i fix this type of error: “Uncaught ReferenceError: check_email is not defined onblur”

<!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>

I am still a starter in PHP and JQuery I want to know how to fix this type of error? 我仍然是PHP和JQuery的首发我想知道如何修复这种类型的错误? I check it from firebug. 我从萤火虫检查它。 The flow is that after the user done typing the email it will check automatically from the database if it exist or not. 流程是在用户完成键入电子邮件后,它将自动从数据库中检查是否存在。 And the expected output does not show in my page. 并且预期的输出不会显示在我的页面中。

You're missing quotes on selectors, seperating parameters in the ajax function with semicolons and not comma's etc. 你在选择器上缺少引号,用分号而不是逗号分隔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;
       }

Add another <script> tag, it's not right way to add js source files and also code in the same tags. 添加另一个<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>
<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>

EDIT 编辑

$.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);
                }
    <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>

add to onblur="javascript:checkEmail(); and i made a function a bit different, declared it to var. 添加到onblur="javascript:checkEmail();并且我使函数有点不同,将其声明为var。

Place your jquery source on a separate script. 将jquery源放在单独的脚本上。 Make sure you put a closing tag (not a self-closing tag) for script. 确保为脚本添加结束标记(不是自动关闭标记)。

Use commas for arguments, not semi-colon. 使用逗号作为参数,而不是分号。

Try this: 尝试这个:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM