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

时间:2018-07-26 08:38:41

标签: php jquery ajax

代码:

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> :set prompt >>
>>:set prompt2 |
>>:{
|let foo :: [Int]
|    foo = [37, 9, 18]
|:}
>>foo
[37,9,18]

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>

我已经在bootstrap模态中创建了登录表单,并希望使用ajax登录,但是问题是,当我单击登录按钮时,它无法正确重定向到Student / dashboard.php。该页面即Student / dashboard.php在模型内部显示。我不知道为什么会出问题。因此,如何在student / dashboard.php中重定向?请帮助我。

谢谢

2 个答案:

答案 0 :(得分:0)

尝试

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

JS:

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

答案 1 :(得分:0)

山姆

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

在这种情况下不起作用。

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

登录后,您需要在会话中维护一个值,然后在信息中心中检查该值。

相关问题