0

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

sam orten
  • 206
  • 3
  • 16
  • 1
    Possible duplicate of [How to manage a redirect request after a jQuery Ajax call](https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – peeebeee Jul 26 '18 at 08:41

2 Answers2

0

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>
kallosz
  • 521
  • 3
  • 10
0

Sam,

A proper way is to redirect it from client side instead of redirecting it through server.

It wont work in this case.

Try to send some response from server and depending upon that do a redirect through window.location.href.

You need to maintain a value in session after logging in and in dashboard check the value.

Vishal Shinde
  • 26
  • 1
  • 4