What are some common techniques for handling form submissions and displaying feedback messages in PHP web development?

When handling form submissions in PHP web development, it is common to use techniques such as checking for form submission, validating input data, processing the form data, and displaying feedback messages to the user. One way to display feedback messages is by using sessions to store and retrieve messages that can be displayed on the next page load.

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate input data
    $name = $_POST["name"];
    
    if (empty($name)) {
        $_SESSION["error"] = "Name is required!";
    } else {
        // Process form data
        // Save data to database, send email, etc.
        
        $_SESSION["success"] = "Form submitted successfully!";
    }
    
    // Redirect to prevent form resubmission
    header("Location: /form_page.php");
    exit();
}

// Display feedback messages
if (isset($_SESSION["error"])) {
    echo "<div class='error'>" . $_SESSION["error"] . "</div>";
    unset($_SESSION["error"]);
}

if (isset($_SESSION["success"])) {
    echo "<div class='success'>" . $_SESSION["success"] . "</div>";
    unset($_SESSION["success"]);
}