PHP,Ajax登录表单问题

时间:2017-10-16 06:33:36

标签: php jquery ajax mysqli

我正在使用ajax控件开发php登录表单,实际上我是这个概念的新手。我从那里有index.php,我通过ajax方法发送用户名和密码。 Ajax函数将调用ajaxAdminLogin.php,在进行用户验证过程后,它不会重定向到dashboard.php。这是我的代码请帮帮我

的index.php

<body>
    <div class="loginbg">
        <div class="container">
            <div class="row content-area">
                <div class="col-md-6 company-profile">
                    <div id="" class="adminLoginText">
                        <h1>Admin Login</h1>
                    </div>
                    <div id="" class="comapny-text">                            
                        <h3>Uni Web Tech</h3>
                        <p>Website Designing & Development</p>
                    </div>
                </div>
                <div class="col-md-6 login-form">
                    <div id="" class="login-form-area">
                        <form method="post">
                            <div class="form-group">
                                <label for="admin-username">User Name</label>
                                <input id="admin-username" class="form-control" type="text" name="adminUsername" required>
                            </div>
                            <div class="form-group">
                                <label for="admin-password">Password</label>
                                <input id="admin-password" class="form-control" type="password" name="adminPassword" required>
                            </div>                              
                            <button type="button" class="btn btn-primary" name="adminLogin" id="adminLogin">Login</button>
                        </form>
                        <a href="#">Forgot Password?</a>
                        <p style="margin-top:30px;text-align:center;color:#ea6957;" id="login-error"></p>
                    </div>                      
                </div>                  
            </div>              
        </div>
    </div>
</body> 
<script type="text/javascript" src="js/adminloginJs.js"></script>

adminloginJs.js

<script>
    $("document").ready(function () {
        $("#adminLogin").click(function () {
            var adminUsername = $("#admin-username").val().trim();
            var adminPassword = $("#admin-password").val().trim();

            if (adminUsername != "" && adminPassword != "")
            {
                $.ajax({
                    type: 'POST',
                    url: "ajax/ajaxAdminLogin.php?adminUsername=" + adminUsername + "&adminPassword=" + adminPassword,
                    cache: false,
                    success: function (message) {
                        $("#login-error").text("");
                        $("#login-error").text(message);
                        if ($("#login-error").text().trim() == "Done") {
                            window.location.href = "http://localhost/unidashboard/dashboard.php";
                        }
                    }
                });

                /*$.post("ajaxAdminLogin.php",{adminUsername: adminUsername, adminPassword: adminPassword})
                 .done(function(data){
                 if(data.trim() == "Done"){
                 window.location = "dashboard.php";
                 }
                 else{
                 $("#login-error").text(data);
                 }
                 });*/
            } else
            {
                $("#login-error").text("");
                $("#login-error").text("Please enter Username and Password");
            }
        });
    });
</script>

ajaxAdminLogin.php

<?php
session_start();
/* Uni Web Tech Online Exam DB Connection */
include("includes/dbConnection.php");
/* Tracking User IP Address */

function get_ip_address() {
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                $ip = trim($ip); // just to be safe
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
                    return $ip;
                }
            }
        }
    }
}

$AdminIPAddress = get_ip_address();

$username = $_POST['adminUsername'];
$password = $_POST['adminPassword'];
/* Fetching User Data */
$LoginSql = "select AdminId, Email, Password, Role, Status from adminusers where AdminId =  '$username' or Email = '$username'";
$result = $uni_DB_Con->query($LoginSql);

if ($result->num_rows > 0) {
    // fetching user details
    while ($row = $result->fetch_assoc()) {
        //verifying Enabled or Disabled
        if ($row['Status'] == "Enable") {
            // verifying username
            if ($row['AdminId'] == $username || $row['Email'] == $username) {
                if ($row['Password'] == $password) {
                    $AdminId = $row['AdminId'];

                    /* updating user login status 0 to 1 in users table */
                    $loginStatusSql = "UPDATE adminusers SET LoginStatus=1 WHERE AdminId = '$AdminId'";
                    $uni_DB_Con->query($loginStatusSql);

                    /* Creating session */
                    $_SESSION["AdminId"] = $AdminId;
                    $_SESSION["Role"] = $row['Role'];

                    /* Details for Activity */
                    $Login = Date("d - F - Y H:i:s");
                    $loginActivitySql = "INSERT INTO admin_activity(AdminId, AdminIPAddress, Login, Logout) VALUES ('$AdminId','$AdminIPAddress','$Login','0')";

                    $uni_DB_Con->query($loginActivitySql);


                    /* After storing login date and time navigating to dashboard page */
                    echo "Done"; /* redirect done from adminlogin.js */
                } else {
                    echo "Invalid Password.";
                }
            } else {
                echo "Invalid Username.";
            }
        } else {
            echo "Sorry, You are disabled. Please contact admin.";
        }
    }
}
?>

请告诉我,如果我有任何错误。

1 个答案:

答案 0 :(得分:1)

检查下面脚本中的评论部分可能是因为你传递了一个查询,你在服务器中使用帖子来获取数据

       <script>
                    $("document").ready(function () {
                        $("#adminLogin").click(function () {
                            var adminUsername = $("#admin-username").val().trim();
                            var adminPassword = $("#admin-password").val().trim();

                            if (adminUsername != "" && adminPassword != "")
                            {
                                $.ajax({
                                    type: 'POST',
////From Here Replace and add these /////

url: "ajax/ajaxAdminLogin.php,
    data:{'adminusername':adminUsername,'adminPassword':adminPassword}
         dataType: 'HTML',

//////////////////////////////////////

                                    cache: false,
                                    success: function (message) {
        console.log(message)
                                        $("#login-error").text("");
                                        $("#login-error").text(message);
                                        if ($("#login-error").text().trim() == "Done") {
                                            window.location.href = "http://localhost/unidashboard/dashboard.php";
                                        }
                                    }
                                });

                                /*$.post("ajaxAdminLogin.php",{adminUsername: adminUsername, adminPassword: adminPassword})
                                 .done(function(data){
                                 if(data.trim() == "Done"){
                                 window.location = "dashboard.php";
                                 }
                                 else{
                                 $("#login-error").text(data);
                                 }
                                 });*/
                            } else
                            {
                                $("#login-error").text("");
                                $("#login-error").text("Please enter Username and Password");
                            }
                        });
                    });
                </script>