Search code examples
phphttp-redirectheader

PHP Header Redirect Does not Work Within The Ajax Request


After user's filled the input fields and has submitted I use an ajax request to complete the sign up process.In this register.php I'd like to redirect the user to another page after I inserted their info to the database. Everything is fine and nothing is outputted until header() function. The code below does not redirect and as if nothing happened the callback function of ajax request is processed. I am really stuck here, I don't know what to do.I can try javascript solutions but I'd prefer php solution over that one. Code:

header("Location: http://localhost/profil.php", true, 307); 
exit();

Solution

  • You can use JavaScript to redirect in the ajax success callback function:

    success: function(data) { 
       window.location.href = 'http://localhost/profil.php';
    }
    

    It can't be done with a PHP header redirect, because that will only redirect the ajax call, not the original browser window.