0

在我输入输入后,文本框再次显示,我不知道如何修复。这是输出的屏幕截图:

在此处输入图像描述

是否在开启模糊后再次显示

这是我的代码,我不知道如何解决这个问题。虽然我的功能运作良好

<!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"></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>
    </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 user_email from tbl_users WHERE user_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>
4

2 回答 2

1

像这样更改您的代码。问题是您将数据发布到您所在的同一页面,并且您将整个代码作为 AJAX 请求的响应。因此,如果您在 POST 上退出执行,您将只会收到相关消息。

 <?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 user_email from tbl_users WHERE user_email=:email LIMIT 1');
            $stmt->execute(array(':email'=>$email));

            if($stmt->rowCount()>0) {
                echo 'E-mail already use.';
            }
            else {
                echo 'E-mail not use.';
            } 
            exit;  
        }
        ?><!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"></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>
    </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>

    </form>
    </body>
    </html>
于 2012-08-30T04:16:16.057 回答
-1

在添加check_email之前的函数中return false

$("#txtEmail").hide();

这是你的意思吗?

于 2012-08-30T04:15:20.047 回答