I have a file that, when called, should redirect to another location. In order to make sure that nothing is sent before the header, I call a file whose only content is the header. I also tried to redirect with other methods, but nothing seems to work. Here is the code:
first there is an ajax call that (successfully) sends some data to file1.php
$.ajax({
url: "file1.php",
type: "POST",
data: {id:10},
success: function(){
},
error: function (){
}
});
the file1.php gets the data and calls bridge.php
<?
if (isset($_POST['id'])) {
$selected_id = $_POST['id'];
}
require('./code/bridge.php');
?>
the file bridge.php should redirect to the new url. I tried the following (not simultaneously) 1.
<script>location.href='https://www.google.al/?gws_rd=cr,ssl&ei=l82QVqyAHIb6O5bJs6gK'</script>
2.
<?
echo '<META HTTP-EQUIV="Refresh" Content="0; URL= https://www.google.al/?gws_rd=cr,ssl&ei=l82QVqyAHIb6O5bJs6gK">';
?>
3.
<?
header("location: /edit_document");
?>
4.
<?
ob_start();
header("location: /edit_document");
?>
I am using firebug to see the response. It gives a status 200 (request was fulfilled). POST gives the right parameter that I sent. Response gives the page where I am trying to redirect. But redirect won't happen.
I reduced the code to this point to eliminate all things that might cause problems. I know you might be wondering why would I write a code that seems to do nothing. But if it doesn't work like this, I doubt I could catch the problem with lots of other calls. Also, I intended to use header for redirecting, but tried other options out of desperation.
Try making AJAX request to obtain redirect URL and then use it for redirect:
$.ajax({
url: "file1.php",
type: "POST",
data: {id:10},
success: function(url){
window.location.href = url;
},
error: function (){
}
});
bridge.php:
<?php
print "https://www.google.al/?gws_rd=cr,ssl&ei=l82QVqyAHIb6O5bJs6gK";
?>
Use Javascript or Jquery redirection ... // Behaviour Like HTTP redirect window.location.replace("http://stackoverflow.com");
// Behaviour Like clicking on a link window.location.href = "http://stackoverflow.com";
People want to use AJAX
so that the page doesn't redirect when submitting form. Strangely you want the opposite but using AJAX
. Why don't you just create a simple form like this and submit the form when you need to redirect.
<form name="myForm" action="file1.php" method="post">
<input type="hidden" name="id" value="10" />
</form>
And just replace your code of ajax ($.ajax({...});
) with this.
document.myForm.submit();
If your form has id="myForm"
instead of name="myForm"
, this will provide the same result:
document.getElementById("myForm").submit();
And if you want to modify the id dynamically from the js, you can do it this way (put the code before submit()
) :
document.myForm.id.value = 20;