You could use a function similar to:
function redirect($url) {
header('Location: '.$url);
die();
}
Worth noting, you should use a die() or exit() function to prevent further code execution.
Note that it just makes no sense to output large chunks of HTML if you are going to redirect. Therefore you have to move the form handling code above all HTML. As a side effect it will mitigate the notorious "Headers already sent" error.
Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/
This guide includes reasons for using die() / exit() functions in your redirects, as well as when to use ob_flush() vs ob_start(), and some potential errors that the others answers have left out at this point.
You could use a function similar to:
function redirect($url) {
header('Location: '.$url);
die();
}
Worth noting, you should use a die() or exit() function to prevent further code execution.
Note that it just makes no sense to output large chunks of HTML if you are going to redirect. Therefore you have to move the form handling code above all HTML. As a side effect it will mitigate the notorious "Headers already sent" error.
Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/
This guide includes reasons for using die() / exit() functions in your redirects, as well as when to use ob_flush() vs ob_start(), and some potential errors that the others answers have left out at this point.
You can conditionally redirect to some page within a php file....
if (ConditionToRedirect){
//You need to redirect
header("Location: http://www.yourwebsite.com/user.php");
exit();
}
else{
// do something
}
Redirect to PHP page after IF statement
PHP contact form redirect to another page
Can you provide some code?
More on reddit.comVideos
Hello! I am trying to have the user be redirected to a PHP script after they have submitted the form. Currently, if someone submits the form I have it set up to display an echo message. I'm not sure what condition I should be using to redirect to this page. I'd love to have it be redirecting to the new page instead of echo 'You have successfully registered, you can login'; - Much appreciate the help!
if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)')) {
`// hash the password when someone submits`
`$password = password_hash($_POST['password'], PASSWORD_DEFAULT);`
`$stmt->bind_param('sss', $_POST['username'], $password, $_POST['email']);`
`$stmt->execute();`
`echo 'You have successfully registered, you can now login!';`
} else {
`// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.` `echo 'Could not prepare statement!';`
If there is a better way to do this to run in the background I'm more than happy to look at those too!