</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/23807411/redirect-with-php-after-ajax-call" dir="ltr">Redirect with PHP after ajax call</a>
<span class="question-originals-answer-count">
(5 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2017-08-08 11:52:07Z" class="relativetime">2 years ago</span>.</div>
</div>
</aside>
I want to redirect inside a php file that is called using ajax. Inside the file I write the redirection. But the redirect code is not working.
This is the code for the ajax file:
<?php
ob_start();
include 'admin/includes/front_controller.php';
if (isset($_POST['id'])) {
$job_id = intval($_POST['id']);
$job_id = 30;
$jobdetail = $db->getTableWhere("jobs", "jbid", $job_id);
$count = count($jobdetail);
if ($count <= 0) {
header("Location: jobs.php");
exit();
}
} else {
echo "There is an error processing your request";
}
?>
This is the ajax call I am using:
<script type="text/javascript">
$(".job-btn").on('click', function (e) {
e.preventDefault();
var id = $(this).data('id');
//alert(id);
var url = "<?php echo SITE_URL; ?>" + "jobdetail.php";
var info = 'id=' + id;
$.ajax({
type: "POST",
url: url,
data: info,
success: function (data) {
alert(data);
},
error: function (data) {
//alert(data.responseText);
//alert("Error occured in showing details");
}
})
});
</script>
</div>
Redirect using js in the success function
success: function (data) {
alert(data);
window.location ="jobs.php";
},
Instead of:
header("Location: jobs.php");
inside php code, return the url in response
to the ajax. And use window.location
in success
like:
success: function (response) {
window.location = response;
}