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 Overflow
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
How to use PHP to Redirect to another page after form submition? - PHP Coding Help - PHP Freaks
June 6, 2024 - Hello everyone, I am trying to add a redirect option to my mail sending code, but it seems not to work. Pls, can someone help me ? I tried to add a redirect using header("Location: thanks.php"); but it does not work. Form and mail sending files are attached below https://drive.google.com/file/d/1...
Discussions

How to redirect to another page after submit in php - Stack Overflow
How to redirect to a different page after user clicks Add: I am trying to redirect the user to a different page called (Thank_you.php) after the user fills out the form fields and clicks Add to sub... More on stackoverflow.com
🌐 stackoverflow.com
December 7, 2016
How to redirect to another page using PHP - Stack Overflow
I'm building a website which includes a login page. I need to redirect the user to their profile page once they've logged in successfully, but I don't know how to do that in PHP (It's my first site... More on stackoverflow.com
🌐 stackoverflow.com
php on submit redirect to another page - Stack Overflow
I have a form in my html document and when press submit button, the form calls the file form.php. Now in the form.php, i want to redirect it back to my home page and reset the previous filled form. I More on stackoverflow.com
🌐 stackoverflow.com
Form redirect to thanks.php
Andrew Thompson is having issues with: Hi Guys, I am having a problem with this. I am expecting this code to execute, and the user to be redirected to thanks.php after the da... More on teamtreehouse.com
🌐 teamtreehouse.com
10
January 26, 2016
🌐
SitePoint
sitepoint.com › php
How to use PHP to Redirect to another page after form submition? - PHP - SitePoint Forums | Web Development & Design Community
June 2, 2024 - Hello everyone, I am trying to add a redirect option to my mail sending code, but it seems not to work. Pls, can someone help me ? Below is my form: <!doctype html> <html class="no-js" lang="fr"> <head> <body> <fo…
🌐
FormGet
formget.com › home › php › php: redirect to url after form submission
PHP Redirect to URL After Form Submission | FormGet
June 16, 2014 - Redirection benefits you in many ways. Now in PHP, redirection is done by using header() function as it is considered to be the fastest method to redirect traffic from one web page to another.
🌐
Talkerscode
talkerscode.com › howto › how-to-redirect-to-another-page-in-php-after-submit.php
How To Redirect To Another Page In PHP After Submit
July 1, 2023 - In this tutorial we will show you ... button in php we can easily find out by isset() method and for redirect we need to use header() function....
🌐
Adobe Support Community
community.adobe.com › home › app communities › dreamweaver › questions › how do i redirect a page in php after user submission?
How do I redirect a page in PHP After User Submission? | Community
June 17, 2012 - Then if the user submitted the form, they will get redirected, if not they see the page. Make sense? ... Actually, using the header function is not the only way. It would be an alternative to echo javascript to change the document location. Like so: ... It's just another way! ... Hey... Thanks I appreciate an alternative method. It's nice to have choices! I still have quite a bit to learn about PHP...
🌐
SR CRAFT BLOG
srcraftblog.com › how-to-redirect-to-another-page-in-php-after-submit
how to redirect to another page in php after submit
How to redirect a url? or how to redirect a page to another page? It is basic requirements of any website that was made in PHP or other languages. Today Redirection is an essential part of recent website. In redirection, you can actually divert ...
Find elsewhere
🌐
Java2s
java2s.com › Tutorials › PHP › Language_Basics › 3580__PHP_Form_Redirect.htm
PHP Tutorial - PHP Redirect after a Form Submission
Here's a quick example of a form handler script that redirects to a thank - you page: <?php //from ww w .j a va2 s .c om if ( isset( $_POST["submitButton"] ) ) { // (deal with the submitted fields here) header( "Location: thanks.html" ); exit; } else { displayForm(); } function displayForm() { ?> <!DOCTYPE html5> <html> <body> <form action="index.php" method="post"> <label for="firstName">First name</label> <input type="text" name="firstName" id="firstName" value="" /> <label for="lastName">Last name</label> <input type="text" name="lastName" id="lastName" value="" /> <input type="submit" name="submitButton" id="submitButton" value= "Send Details" /> </form> </body> </html> <?php } ?>
🌐
Stack Overflow
stackoverflow.com › questions › 41006832 › how-to-redirect-to-another-page-after-submit-in-php
How to redirect to another page after submit in php - Stack Overflow
December 7, 2016 - //include the header $page_title = 'Add Company'; include ('includes/header.html'); echo '<h1>Add Company</h1>'; require_once ('../mysqli_connect.php'); if ($_POST['submitted']){ $company_name=$_POST['company_name']; $product_type=$_POST['product_type']; $city=$_POST['city']; $state=$_POST['state']; $query="INSERT INTO Company (Company_Name, Product_Type, City, State) Values ('$company_name', '$product_type', '$city','$state')"; $result=@mysqli_query ($dbc, $query); if ($result){ //echo "<center><p><b>Thank you.</b></p>"; echo "<a href=Thank_you.php>The company has been added</a></center>"; //
🌐
Medium
medium.com › @benjamincrozat › redirect-users-to-another-page-in-php-the-right-way-633ae8954166
Redirect users to another page in PHP, the right way | by Benjamin Crozat | Medium
October 7, 2022 - do_something(); // Text output should come after headers. // This code won't be executed either. echo 'Hello, World!'; By default, adding a “Location” header will result in a 302 Moved Temporarily redirect. But most of the time, we need 301 Moved Permanently redirections (the most common use case is for SEO purposes). Since PHP 5.4, we can use the `http_response_code()ˋ function: <?php http_response_code(301); header('Location: https://example.com/some/page');
Top answer
1 of 10
26
Hi Andrew, php has something called output buffering. When output buffering is turned on then no output will be sent except for headers. The output is instead stored in a buffer, generally for further processing before it is sent. The php environment within workspaces has output buffering turned on. This is why it works in the video and the echo statements do not prevent the header redirect from working. You likely have output buffering turned off in your local environment. To check this, you can run the phpinfo() function inside your script. http://php.net/manual/en/function.phpinfo.php php You can read more about the constant I passed in at the link above. Basically, it's to reduce the amount of output to make it easier to find what you're looking for. You can run this both locally and in workspaces to see the difference. You want to look for output_buffering in that table. Or you can look for it in your php.ini file. This is the file where it's initially set. The output_buffering directive will turn on or off buffering across all files. You can turn on output buffering as needed within your scripts by calling the ob_start() function. http://php.net/manual/en/function.ob-start.php So for you to get this working locally with the echo statements in place, you either need to change the outputbuffering value to On (4096 seems to be the recommended value) in your php.ini file or you could place a call to obstart() before your echo statements and that should get it working.
2 of 10
12
Worked for me ```php "; $emailbody = ""; $emailbody .= "Name " . $name . "\n"; $emailbody .= "Email" . $email . "\n"; $emailbody .= "Details " . $details . "\n"; echo $email_body; echo ""; // To Do: Send email header("location:thanks.php"); obflush(); obend_clean(); ?> ```
🌐
freeCodeCamp
forum.freecodecamp.org › t › redirect-to-original-page-after-php-sumbmit-contact-form-message-3-columns-bootstrap-4 › 143898
Redirect to original page after php sumbmit contact form message + 3 columns Bootstrap 4 - The freeCodeCamp Forum
August 25, 2017 - I’ve been searching like crazy to figure this out. I have a contact form at the bottom pf a page http://leggacysoccer.com/#section4 After a successful transmission the message “Contact form successfully submitted. Thank…
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-make-a-redirect-in-php
How to make a redirect in PHP? - GeeksforGeeks
July 12, 2025 - Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) ...
🌐
Reddit
reddit.com › r/phphelp › redirect to php page after if statement
r/PHPhelp on Reddit: Redirect to PHP page after IF statement
April 29, 2020 -

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!

🌐
Stack Overflow
stackoverflow.com › questions › 34357604 › how-can-i-redirect-to-another-page-after-submit-button-pressed
php - How can I redirect to another page after submit button pressed? - Stack Overflow
assuming you have a form and its posting to that if block successfully, then that should work header("Location: index.php"); - redirects to the index.php. Any errors when you try ? ... Did you specify action attribute of your form correctly? Do you know if you post to this code at all? Also good practice is to put exit(); after your redirect. ... are you getting an error message? Have you checked log files? It could be that you have output before the header statement, which would prevent redirecting. ... Hello, my code was totally fine, but at the beginning of the page I did something wrong, so the redirecting did not work.
🌐
Benjamin Crozat
benjamincrozat.com › home › blog › php redirect: how to send users to another page
PHP redirect: how to send users to another page
March 20, 2026 - To redirect in PHP, send a Location header and stop the script immediately with exit. PHP’s header() manual notes that Location defaults to 302 unless you already set a 3xx status code, so choose the right one on purpose.
🌐
SitePoint
sitepoint.com › php
How do I redirect contact form after submission? - PHP - SitePoint Forums | Web Development & Design Community
March 5, 2010 - Hello, Currently once my contact form is submitted the page goes to the form.submit.php page that is defined in the action of the form (action=“form.submit.php”). What I want it to do is to stay on the contact page and …