0

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

sachleen
  • 28,887
  • 7
  • 71
  • 71
user2216325
  • 61
  • 2
  • 11
  • 1
    We can't help without seeing the code in question. You could redirect to a success page or have a refresh time. What research have you done? – UnholyRanger Mar 28 '13 at 22:00
  • The `Location` header tells the browser that the page can be found at another location. It does not make sense to show a success page *with* a `Location` header, because it will not be displayed. – Sverri M. Olsen Mar 28 '13 at 22:10
  • You should consider using javascript instead of dealing with headers – Wistar Mar 28 '13 at 22:23

4 Answers4

1

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>
What have you tried
  • 10,308
  • 4
  • 28
  • 45
1

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']);

}
MichaelRushton
  • 10,294
  • 3
  • 38
  • 59
0

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.

sachleen
  • 28,887
  • 7
  • 71
  • 71
  • I tried that but it refreshes even if no username or password is typed in. I need it to redirect if username and password is correct. – user2216325 Mar 28 '13 at 23:44
  • This line replaces you header redirect.. You still need to do your validation logic, only echo this after displaying success. – sachleen Mar 28 '13 at 23:46
  • How do i do that? Sorry i'm new to php. So the user logs in at login.html. Then it goes to login.php which displays if login was successful. I need it to redirect to another page if login was successful – user2216325 Mar 28 '13 at 23:48
0

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.

pedro cepeda
  • 41
  • 1
  • 6