How to redirect after submitting form in PHP?

Member

by lottie , in category: PHP General , 2 years ago

How to redirect after submitting form in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lily , a year 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.

Member

by shyann , 10 months ago

@lottie 

To redirect after submitting a form in PHP, you can use the header() function to send an HTTP redirect header. Here's an example:

1


In this example, after performing the necessary form processing, the header() function is used to send an HTTP redirect header with the location of the new page (new_page.php). The exit statement is used to terminate the current script execution immediately to prevent any further processing.


Make sure to include this redirection code before any output is sent to the browser to avoid any "headers already sent" errors.