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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line