douye9822 2018-07-26 08:38
浏览 91
已采纳

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

code:

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

candidate.php

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

I have created login form inside the bootstrap modal and wants to login using ajax but the problem is that when I click on login button it does't redirect to student/dashboard.php properly. This page i.e. student/dashboard.php are showing inside the model. I do't know why what is the problem. So, How can I redirect in student/dashboard.php ?Please help me.

Thank You

  • 写回答

2条回答 默认 最新

  • doujiejujixi27244 2018-07-26 09:01
    关注

    try this

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算