登录后我无法重定向用户

时间:2019-09-09 06:46:49

标签: javascript php mysql redirect

我有一个登录页面,在用户登录后,该页面会从用户那里获取用户名和密码。我收到成功登录的响应。但我无法重定向到index.php

我收到控制台消息,提示用户已成功登录,但是随后该页面无法在其他页面上重定向。

我的JS文件是这样的。

$(document).ready(function() {
    //alert('ss');
	$("#login-form").submit(function(e){
		e.preventDefault();
	  
		var username = $("#email").val();
		var password = $("#password").val();
		var isValid = true;
		var re =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
		if(!re.test($("#email").val())){
			isValid = false;
			$("#message").html("Please enter valid email.");
		}
		
		if($('#remember').is(':checked')){
			$.cookie('adminCookie', $("#loginUser").val(), { expires: 7 });
		}
		else{
			if($.cookie('adminCookie') == null) { 
				$.removeCookie("adminCookie");
			}	
		}
		if(password == ""){
			isValid = false;
			$("#message").html("Please enter password.");
		}
	  
		if(isValid){
			
			$.ajax({
				type : "POST",
				url : "process/login.php?rand="+Math.random(),
				data : {
						username : $("#email").val(),
						password : $("#password").val(),
					}
			}).
			done(function(response){
                //alert(response);
				var JSONArray = $.parseJSON(rawJSON);

				console.log(response);
				if(jsonObj.success=="1"){
                    
                    window.location("index.php");
				}else{
					$("#userDiv").addClass("form-group has-error");
					$("#passDiv").addClass("form-group has-error");
					$("#passWarning").css("display","");
					$("#passWarning").html("Invalid User Name or Password");
					$.when($('#passWarning').fadeOut(5000)).done(function() {
						$("#userDiv").attr('class', 'form-group');
						$("#passDiv").attr('class', 'form-group');
					});
				}
			}).fail(function(){
					
			}).always(function() {
					
			});	
		}
	});
});

我的PHP文件看起来像这样。

<?php
include "../connection.php";
extract($_POST);
$data = array();
$resArr = array();
if (isset($_POST['username']) && isset($_POST['password'])) {
    $check_user = mysqli_query($conn, "select * from cut_users where email like '$username' and password like '$password' and user_role like 'A'");
    if ($check_user) {
        if (mysqli_num_rows($check_user) > 0) {
            $message = "Login successfully.";
            $success = 1;
            while ($row = mysqli_fetch_array($check_user)) {
                if ($row['is_active'] == "0") {
                    $message = "Your account is blocked. Please contact admin.";
                    $success = 0;
                }
                $data["id"] = $row['cut_users_id'];
                $data["name"] = $row['name'];
                $data["emailID"] = $row['email'];
                $data["device"] = $row['device_type'];
                $data["is_reset"] = $row['is_reset'];
                $_SESSION['userID'] = $row['cut_users_id'];
                $_SESSION['name'] = $row['name'];
                $_SESSION['emailID'] = $row['email'];
            }
            $resArr = array("success" => $success, "data" => $data, "message" => $message);
            
        } else {
            $resArr = array("success" => 0, "data" => array(), "message" => "Email or password invalid.");
        }
    } else {
        $resArr = array("success" => 0, "data" => array(), "message" => "Something went wrong!!!" . mysqli_error($conn));
    }
} else {
    $resArr = array("success" => 0, "data" => array(), "message" => "Parameter Missing");
}
header('Content-Type: application/json');
echo str_replace("\/", "/", json_encode($resArr, JSON_UNESCAPED_UNICODE));
?>

3 个答案:

答案 0 :(得分:3)

1)为什么要使用变量jsonObj而不是response

2)像这样修改您的if statement

if(response.success==1){
     window.location.href="index.php";
}

答案 1 :(得分:1)

window.location不是一个函数,它是一个对象。请更改window.location对象中的href属性以重定向到新的URL。

window.location.href = "./index.php";

答案 2 :(得分:0)

简单重定向到特定文件


if(jsonObj.success==1)
{
window.location.href = "http://www.example.com/index.php";

}