How can the PHP code be optimized to ensure that the form disappears after submission and only a thank you message is displayed?

To optimize the PHP code to ensure that the form disappears after submission and only a thank you message is displayed, you can use a conditional statement to check if the form has been submitted. If it has, display the thank you message; if not, display the form. You can achieve this by setting a variable to track the form submission status.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Form submitted, display thank you message
    echo "Thank you for your submission!";
} else {
    // Form not submitted, display form
    ?>
    <form method="post">
        <!-- Your form fields go here -->
        <input type="submit" value="Submit">
    </form>
    <?php
}
?>