weixin_33686714 2017-10-16 06:33 采纳率: 0%
浏览 32

PHP,Ajax登录表单问题

I am developing php login form using ajax control, actually I am new to this concept. I have index.php from there, I am sending username and password through ajax method. Ajax function will call ajaxAdminLogin.php, After doing user verification process it is not redirecting to dashboard.php. Here is my code please help me

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

please tell me, If I did any mistake.

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2017-10-16 06:56
    关注

    Check The Commented part in the script below maybe its casue your passing a query stirng and your using a post in the server to pick up the data

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名