How can the PHP code be optimized to prevent the need for double-clicking on the submit button?

To prevent the need for double-clicking on the submit button in PHP, you can disable the button after it has been clicked once using JavaScript. This will prevent users from submitting the form multiple times accidentally.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    
    // Disable the submit button after it has been clicked
    echo '<script>document.getElementById("submitBtn").disabled = true;</script>';
}
?>

<form method="post">
    <!-- Form fields go here -->
    <input type="submit" id="submitBtn" value="Submit">
</form>