简体   繁体   中英

How to check availability of user at login or signup using ajax

/* Login File */

<?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 file */

<?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;
        }
    }
?>

How can i return the session msg and check the username is valid or not? i want to check it because im disabled the password filed if the username is not enter or invalid.This is only enabled if data is register to my site.

As for Login file

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

As for ajax file, you should just echo the value

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

Also, you can find the following for your reference. Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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