如何使用ajax在登录或注册时检查用户的可用性

时间:2016-05-04 08:04:04

标签: php ajax

/ *登录文件* /

<?php session_start(); ?>
<html>
    <head>
        <title>Login Page</title>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <script>
            function checkAvailble() {
                jQuery.ajax({
                    url : "check_data.php",
                    data : "user_id=" + $("#user_name").val(),
                    type : "POST",
                    success : function(data){
                        var msg = + $("#session_msg").val();
                        if(msg == 1){
                            $("#password").removeAttr("disabled");
                        }
                        else if (msg == 0){
                            $("#password").attr("disabled","disabled");
                        }
                        else{
                        }
                    },
                    error : function(){} 
                })
            }
            /*$("#password").attr("disabled","disabled");
                $("#user_name").blur(function(){
                    var username = $("#user_name").val();
                    $.ajax({
                        type : "post",
                        url : "check_data.php",
                        data : "user_id = " + username,
                        success : function(msg){

                            if(msg == "Username Is Not Correct"){
                                $("#password").removeAttr("disabled");
                                $("#user_name_msg").css("display","block");
                                $("#user_name_msg").html("Username Is Correct!");
                            }
                            else{
                                $("#password").attr("disabled","disabled");
                                $("#user_name_msg").css("display","block");
                                $("#user_name_msg").html("Username Is Not Correct!");
                            }
                        }
                    })
                });
            })*/
        </script>
        <style type="text/css">
            .login_form{
                width: 500px;
                height: auto;
            }
            .header{
                margin: 30px 50%;
                color : #ccc;
                font-size: 28px;
                font-family: sans-serif;
            }
            body{
                background-color: #fff;
            }
        </style>
    </head>
    <body>
        <?php if(isset($_SESSION['error'])){?>
            <div class="alert alert-danger">
                <?php echo $_SESSION['error'];
                unset($_SESSION['error']); ?>
            </div>
        <?php } ?>

        <div class="container login_form">
            <div class="header">
                LogIn
            </div>
            <div class="row">
                <form role = "form" action="controler.php" method="post">
                    <div class="form-group">
                        <label for = "username">Username</label>
                        <input class="form-control" onBlur="checkAvailble()" id="user_name" placeholder="Username" name="user_name" required="" type="email"></input>
                        <span id="user_status"></span>
                    </div>
                    <div class="form-group">
                        <label for = "username">Password</label>
                        <input class="form-control" id="password" disabled="" placeholder="Password" name="pswd" required="" type="password"></input>
                    </div>
                    <input type="submit" value="Login" class="btn btn-primary" name="btn_login">
                </form>
            </div>
        </div>
    </body>

</html>

/ * ajax文件* /

<?php 
    session_start();
    require_once 'dao.php';
    $d = new dao();

    if(!empty($_POST['user_id'])){
        $sel = $d->select("users","email_id='" . $_POST['user_id'] . "'");
        $result = mysqli_fetch_assoc($sel);
        if($result['email_id'] == $_POST['user_id']){
            $_SESSION['msg'] =  1;
        }
        else{
            $_SESSION['msg'] =  0;
        }
    }
?>

如何返回会话消息并检查用户名是否有效? 我想检查它,因为如果用户名未输入或无效,我已禁用密码。仅当数据注册到我的网站时才会启用此功能。

1 个答案:

答案 0 :(得分:0)

登录文件

success : function(data, status){
                    if(data == 1){
                        $("#password").removeAttr("disabled");
                    }
                    else if (data == 0){
                        $("#password").attr("disabled","disabled");
                    }
                    else{
                    }
                },

至于ajax文件,你应该只回显值

if($result['email_id'] == $_POST['user_id']){
        echo "1";
    }
    else{
        echo "0";
    }

此外,您可以找到以下内容供您参考。Reference