How to redirect after submitting form in PHP?

Member

by lottie , in category: PHP General , 9 months ago

How to redirect after submitting form in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 3 months ago

@lottie 

To redirect after a form submission in PHP, you can use the header function. For example:

1
2
3
4
5
6
7
8
<?php
if (isset($_POST['submit'])) {
  // form submission code goes here
  // redirect to a new page
  header('Location: http://www.example.com/newpage.php');
  exit;
}
?>


This will redirect the user's browser to the specified URL after the form is submitted. Make sure to include the exit statement after the header function, as this will stop the script from executing and prevent any other output from being sent to the browser.


It's also important to note that the header function must be called before any other output is sent to the browser, so make sure to place it at the top of your PHP script.


I hope this helps! Let me know if you have any questions.