How can the issue of PHP code executing without button press be resolved?

Issue: PHP code is executing without a button press due to the code being included in the same file as the form. To resolve this, we can use a conditional check to only execute the PHP code when the form is submitted.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // PHP code to execute when the form is submitted
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <!-- Form fields go here -->
    <button type="submit" name="submit">Submit</button>
</form>