Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 执行登录时重定向无法正常工作_Php_Jquery_Ajax - Fatal编程技术网

Php 执行登录时重定向无法正常工作

Php 执行登录时重定向无法正常工作,php,jquery,ajax,Php,Jquery,Ajax,代码: $(文档).ready(函数(){ $(“#登录”)。单击(函数(事件){ event.preventDefault(); elogin=$(“#elogin”).val(); plogin=$(“#plogin”).val(); $.ajax({ 类型:“POST”, 数据:{“elogin”:elogin,“plogin”:plogin}, url:“candidate.php”, 成功:功能(数据){ $(“.login_success”).html(数据); } }); });

代码:


$(文档).ready(函数(){
$(“#登录”)。单击(函数(事件){
event.preventDefault();
elogin=$(“#elogin”).val();
plogin=$(“#plogin”).val();
$.ajax({
类型:“POST”,
数据:{“elogin”:elogin,“plogin”:plogin},
url:“candidate.php”,
成功:功能(数据){
$(“.login_success”).html(数据);
}
});
});
});
candidate.php

<script>
    $(document).ready(function(){
        $("#login").click(function(event){
            event.preventDefault();
            elogin = $("#elogin").val();
            plogin = $("#plogin").val();
            $.ajax({
                type:"POST",
                data:{"elogin":elogin,"plogin":plogin},
                url:"candidate.php",
                success:function(data){
                    $(".login_success").html(data);
                }
            });
        });
    });
</script>
<div class="login_success"></div>   
<form class="loginLay" id="myform">
    <input type="text" name="elogin" id="elogin" class="form-control1" placeholder = "Enter Your Email"/>
    <input type="text" name="plogin" id="plogin" class="form-control1" placeholder = "Enter Your Password"/>
    <a href="javascript:void(0)" id="forgot">Forgot Password</a>
    <center>
        <input type="submit" name="login" id="login" class="btn btn-primary"/>
    </center>
</form>
试试这个

<?php
    session_start();
    include('config.php');

    $email = $_POST['elogin'];
    $password = $_POST['plogin'];
    $sql = "select * from student where email = '$email' and password = '$password' and status = 'enable'";
    $result = mysqli_query($con,$sql);
    $num_rows = mysqli_num_rows($result);
    if($result)
    {
        if ($num_rows > 0) 
        {
            $sqls = "select * from student where email = '$email' and password = '$password'";
            $results = mysqli_query($con,$sqls);
            while ($rows = mysqli_fetch_assoc($results)) 
            {
                $_SESSION['student_id'] =  $rows["id"];

            }
            header ("Location: student/dashboard.php");
        }
        else 
        {
            echo "<p id='red'>Wrong email or password or may be your account not activated.</p>";
        }   
    }
?>

JS:


$(文档).ready(函数(){
$(“#登录”)。单击(函数(事件){
event.preventDefault();
elogin=$(“#elogin”).val();
plogin=$(“#plogin”).val();
$.ajax({
类型:“POST”,
数据:{“elogin”:elogin,“plogin”:plogin},
url:“candidate.php”,
成功:功能(数据){
if(数据类型!=='object'){
data=JSON.parse(数据);
}
if(data.redirect){
窗口.位置.替换(数据.重定向);
}否则{
$(“.login_success”).html(“

”+data.error+”

”); } } }); }); });
Sam

正确的方法是从客户端重定向,而不是通过服务器重定向

在这种情况下是行不通的

尝试从服务器发送一些响应,并根据需要通过window.location.href重定向

您需要在登录后在会话中维护一个值,并在仪表板中检查该值。

该值可能重复
<?php
    session_start();
    include('config.php');

    $email = $_POST['elogin'];
    $password = $_POST['plogin'];
    $sql = "select * from student where email = '$email' and password = '$password' and status = 'enable'";
    $result = mysqli_query($con,$sql);
    $num_rows = mysqli_num_rows($result);
    if($result)
    {
        if ($num_rows > 0) 
        {
            $sqls = "select * from student where email = '$email' and password = '$password'";
            $results = mysqli_query($con,$sqls);
            while ($rows = mysqli_fetch_assoc($results)) 
            {
                $_SESSION['student_id'] =  $rows["id"];

            }
            if (!isset($_POST)) {
                header ("Location: student/dashboard.php");
            } else {
                echo json_encode(array('redirect' => 'student/dashboard.php'));
            }
        }
        else 
        {
            echo json_encode(array('error' => 'Wrong email or password or may be your account not activated.'));
        }
    }
?>
<script>
    $(document).ready(function(){
        $("#login").click(function(event){
            event.preventDefault();
            elogin = $("#elogin").val();
            plogin = $("#plogin").val();
            $.ajax({
                type:"POST",
                data:{"elogin":elogin,"plogin":plogin},
                url:"candidate.php",
                success: function(data) {
                    if (typeof data !== 'object') {
                        data = JSON.parse(data);
                    }

                    if (data.redirect) {
                        window.location.replace(data.redirect);
                    } else {
                        $(".login_success").html('<p id="red">' + data.error + '</p>');
                    }
                }
            });
        });
    });
</script>