Reputation: 137
I want this PHP code to redirect to the previous page and refresh automatically... I understand that with JavaScript I can go history back but it won't refresh. Please help.
My code:
<?php
$con=mysqli_connect("host","user","pass","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//post result to db
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysqli_fetch_assoc($result_set);
$old_total = $row['points'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>
Upvotes: 0
Views: 538
Reputation: 157344
Get the url in a session, when you want to redirect, just use that url and redirect the user and unset the session var
//Track this on the page you want to redirect your code
$_SESSION['prev_url'] = $_SERVER['REQUEST_URI'];
On the next page use this
//When you want to redirect to previous page
header('Location: '.$_SESSION['prev_url']);
exit;
Be sure you are declaring session_start()
at the top of the page
Upvotes: 3
Reputation: 1684
you can use javascript code :
window.history.go(+1)
window.history.go(-1)
or jquery code:
history.go(1);
history.go(-1);
Upvotes: 0
Reputation: 3542
add
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
Upvotes: 0
Reputation: 22233
To redirect with php:
<? header("location: http://.........."); ?>
Please note that before this instruction you mustn't print html, if some html is printed your header will not be sent
Upvotes: 1