How can PHP scripts be structured to handle form submissions and output in a way that maintains user experience and design integrity on a website?

When handling form submissions in PHP, it is important to structure your scripts in a way that maintains the user experience and design integrity of your website. One way to achieve this is by using conditional statements to check if the form has been submitted, processing the form data, and then displaying the output within the same HTML structure as the rest of the website.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    // Display output within the same HTML structure
    echo "<div class='message'>Thank you for submitting the form, $name! We will contact you at $email.</div>";
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Form Submission</title>
    <style>
        .message {
            color: green;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required><br>
        
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required><br>
        
        <button type="submit">Submit</button>
    </form>
</body>
</html>