php redirect to page with message
By the time the redirect happens and the PHP script depicted by $location
is executed, $message
variable would have been long gone.
To tackle this, you need to pass your message in your location header, using GET
variable:
header("Location: $location?message=success");
And
if(!empty($_GET['message'])) {
$message = $_GET['message'];
// rest of your code
You could also have a look into sessions
session_start();
$_SESSION['message'] = 'success';
header("Location: $location");
then in the destination script:
session_start();
if(!empty($_SESSION['message'])) {
$message = $_SESSION['message'];
// rest of your code
Variables cease to exist after the script ends. Each separate request, each separate PHP script invocation is an entirely new context with no data from any other invocation.
Use sessions to persist data.
you can use sessions
if (mysqli_affected_rows($link) == 1)
{
//succes
$_SESSION['message'] = 'succes';
redirect_to('index.php');
}
and on index
if (!empty($_SESSION['message'])) {
echo '<p class="message"> '.$_SESSION['message'].'</p>';
unset($_SESSION['message']);
}