What are the best practices for handling form submissions in PHP to ensure the correct page is displayed after processing?

When handling form submissions in PHP, it is important to ensure that the correct page is displayed after processing. One way to achieve this is by using conditional statements to determine the action to take based on the form data submitted. By redirecting the user to the appropriate page after processing the form, you can ensure a seamless user experience.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    // Redirect to the appropriate page based on the form data
    if (/* condition for redirecting to page A */) {
        header("Location: pageA.php");
        exit();
    } elseif (/* condition for redirecting to page B */) {
        header("Location: pageB.php");
        exit();
    } else {
        header("Location: defaultPage.php");
        exit();
    }
}