How would you display a success message and then redirect in PHP. I've tried the header("Location: .php");
but it does not display success message. If header("Location: details1.php");
is taken out then success message does display but does not redirect. Any ideas?
Thanks
Page1.php
<html>
<body>
<div id="content">
<?php
if($someCondition){
header("Location: page2.php?message=someMessage");
exit; // don't forget to exit
}
?>
</div>
</body>
</html>
Page2.php
<html>
<body>
<div id="content">
<?php
if(isset($_GET['message']){ // NOTE: THE MESSAGE VAR CAN BE ANY NAME (NOT JUST MESSAGE). JUST MAKE SURE IT MATCHES THE NAME OF THE VARIABLE YOU SENT FROM PAGE1.PHP
$message = htmlspecialchars($_GET['message']);
echo 'the message sent from redirection was: ' . $message;
}
?>
</div>
</body>
</html>
You can use the meta refresh tag to redirect after a second or two:
<meta http-equiv="refresh" content="2;url=http://mysite.com/page.php">
You can pass the success message to the next page, too, and have it display it.
I use this PHP function
function redirects($URL,$msg,$secs){
echo "<html>";
echo "<head>";
echo "<title> Cargando...</title>";
echo "<meta http-equiv='refresh' content ='{$secs};url={$URL}'>";
echo "</head>";
echo "<body>";
echo "<b>{$msg}...</b></br>";
echo "another message and link to go to the site"
echo "</br>";
echo "</body>";
echo "</html>";
}
of course you can style this.
I prefer to use sessions when redirecting with a success/an error message:
one.php
$_SESSION['error'] = 'You lost the Game!';
header('Location: two.php');
exit;
two.php
if (isset($_SESSION['error']))
{
echo $_SESSION['error'];
unset($_SESSION['error']);
}