Right after @mail($email_to, $email_subject, $email_message, $headers);
header('Location: nextpage.php');
Note that you will never see 'Thanks for subscribing to our mailing list'
That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space!
Answer from Eduardo Ponce de Leon on Stack OverflowRight after @mail($email_to, $email_subject, $email_message, $headers);
header('Location: nextpage.php');
Note that you will never see 'Thanks for subscribing to our mailing list'
That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space!
First give your input type submit a name, like this name='submitform'.
and then put this in your php file
if (isset($_POST['submitform']))
{
?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>
<?php
}
Don't forget to change the url to yours.
How to redirect to another page after submit in php - Stack Overflow
How to redirect to another page using PHP - Stack Overflow
php on submit redirect to another page - Stack Overflow
Form redirect to thanks.php
Videos
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
}
You can try the javascript way of redirecting the page:
Example :
$url='http://www.google.com';
echo '<script>window.location = "'.$url.'";</script>';
die;
It may help ...!!
Probably you wrote some echo or print before this header(...). Try ob_start();.
For more info follow this
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!
Simply do:
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
You should redirect with a location header after every post, because otherwise when the user presses the refresh button, it will send again the same form...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
file_put_contents('data.txt', $_POST['data']);
header('location: ' . $_SERVER['PHP_SELF']);
} else {
header('content-type: text/html; charset=utf-8');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="application/x-www-form-urlencoded; charset=utf-8">
<input name="data" type="text" value="<?php echo file_get_contents('data.txt'); ?>"/>
<button>küldés</button>
</form>
<?php
}
Btw. if you want to do proper work, you should try out a php framework instead of this kind of spaghetti code...