Search code examples
phpajaxreload

Page reload not working when the page was called by AJAX


Let's say this is my AJAX

function call_page(id)
{   
     $.ajax({
          type: "POST",
          url: "call_me.php",
          data: "id=" + id,
          success: function(msg){ }                 
     });   
}

call_me.php was successfully called.

Let's say this is my call_me.php content

<?php

$var = $_POST['id'];

if(empty($var))
{
    header("location: call_me.php?id=101");
}
else
{
    do something...
}

?>

Assuming that the first condition 'if(empty($var))' is always satisfied. The page must reload and the go to the else statement. But this is not happening. I guess the page isn't reloading.

How can I correct this problem? Thanks!


Solution

  • Try this

    <?php
    
    $var = $_GET['id'];
    
    if(empty($var))
    {
        // here flag for redirection is set
        echo 1; 
    }
    else
    {
        do something...
    }
    
    ?>
    

    In AJAX:

    $.ajax({
         type: "POST",
          url: "call_me.php",
          data: "id=" + id,
          success: function(msg){ 
            // checking the response is for redirection     
            if(msg == 1)
                // javascript code for redirecting to callme.php
            window.location = "call_me.php?id=101";
          }                 
    });