Reputation: 97
JavaScript file:
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
$("#div").html(r);
}
});
I want to redirect the page to new.php
on success condition so in the ajax.php
file I used header('Location:new.php');
but instead of redirecting to page new.php
it is loading the content of new.php
in the div
.
What changes do I need to make in order to redirect the existing page to a whole new page?
Upvotes: 2
Views: 24220
Reputation: 200
I am not sure why you even want to use jquery / ajax for this function as the whole point using ajax is to be able to reload a part of a page without having to refresh the whole page or redirect to a new page.
Why not just post the form data to the same page and use the PHP to do what is needed and then redirect to new.php using PHP or post the form to new.php and do what is needed with the posted data on that page.
Upvotes: 1
Reputation: 8476
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
window.location = 'new.php';//window.location.href = 'new.php';
//$("#div").html(r);
},
});
Upvotes: 7
Reputation: 5374
By placing a header in the file you're requesting via AJAX, you're simply re-routing your AJAX request to that URL. You're not going to redirect the user's browser, since the AJAX request is happening in the background.
Instead, you need have your redirect in your JavaScript file, specifically inside of the success clause:
//From Pragnesh Chauhan's answer (modified):
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
window.location.href = 'new.php';
//$("#div").html(r); Useless since you're redirecting.
},
});
Upvotes: 3