Home > Backend Development > PHP Tutorial > How to Redirect Users to Another Page After Form Submission in PHP?

How to Redirect Users to Another Page After Form Submission in PHP?

Mary-Kate Olsen
Release: 2024-11-03 12:55:03
Original
1032 people have browsed it

How to Redirect Users to Another Page After Form Submission in PHP?

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>
Copy after login

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:

  • You should never include any text or HTML code before calling header(), as it will break the redirect functionality.
  • The header() function overrides previous headers, so ensure it is called only once with the correct parameters.

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template