PHP form - Registration Successful Message on New Page
Solution 1
You can't redirect to a file after output has been shown. You have two alternate solutions here:
Refresh the page after a timeout while showing the message in the meantime, using a
<meta>
tag:<meta http-equiv="refresh" content="5; url=../login.php">
Output the message after redirection:
<?php header('Location: ../login.php?registered=true');
And in login.php:
<?php if (@$_GET['registered'] == 'true') echo 'You have registered successfully.'
Solution 2
You do a header("Location")
to redirect the page. That is possible but than you want to add a paremeter where you can detect if the registration was successful. Something like:
header( 'Location: ../login.php?action=success' );
And than in you file do this:
if( $_GET['action'] == 'success' ) {
echo "thanks for you registration, log in here";
}
Because if you redirect, the current request vars will not be available in the redirected page. So you need to pass them on to the next.
John_Nil
Updated on February 10, 2020Comments
-
John_Nil about 4 years
I coded a registration form and after every input has been validated , it sends some data to the database and redirects to the login page.
I want to display a message on the login page tho like : Registration Successful - Login Here
if (!validated() { // post error messages } else { echo ' <div class="container"> <div class="page-header"> <h3>Registration Complete</h3> </div> </div>'; header( 'Location: ../login.php' ); }
Login file :
Header - Navbar <div class="container"> <div class="page-header"> <h3>Login To Members Corner</h3> </div> <form> // login form </form> Footer
-
John_Nil about 10 yearsThanks , between you need to put this : @$_GET['action'] , or you will keep getting an undefined error on action
-
Ari about 10 yearsIs
isset()
can replace@
? -
casraf about 10 years@ suppresses errors on the expression it is tied to, in this case @ will supress any errors that will raise from the variable not existing. So it's roughly equivalent to
isset($...) && $...
, fundementally different but in this case provides the same usage. Don't overdo it though, it's bad practice. -
Ari about 10 yearsIs that mean it is better to use
isset()
than suppress the warning? -
casraf about 10 yearsAs long as you know when it's okay to use @ then it's not a big problem, never use it for supressing errors in functions or included pages, but if you need it specifically for things like $_GET not existing or the likes, it's okay - just use proper judgement