Redirecting to Another Page After Form Submission in PHP
Question:
After submitting a form in PHP, how can we redirect the user to another URL while ensuring the form submission is processed successfully?
Answer:
In PHP, you can use the header() function to send a redirect after form submission. Here's how you can do it:
In your PHP script, after processing the form data (e.g., validating inputs, sending an email):
<code class="php">header('Location: nextpage.php');</code>
Replace nextpage.php with the URL of the page you want to redirect to.
Explanation:
The header() function sends a redirect instruction to the browser. It must be called before any other output is generated (e.g., HTML code or echo statements). By sending the redirect instruction, you are telling the browser to load the specified page.
Note:
In your provided code:
You can add the redirect after the @mail() function is called, like this:
<code class="php">@mail($email_to, $email_subject, $email_message, $headers); header('Location: thankyou.html');</code>
This will redirect the user to thankyou.html after the email has been sent successfully.
The above is the detailed content of How to Redirect Users to Another Page After Form Submission in PHP?. For more information, please follow other related articles on the PHP Chinese website!