In the provided PHP code, why is the line 'echo "submitted";' not being executed when the 'weiter' button is clicked?

The issue is that the form action is set to "index.php" which means that when the form is submitted, the page will be refreshed and the "echo 'submitted';" line will not be visible. To solve this issue, you can use AJAX to submit the form asynchronously without refreshing the page. This way, you can still see the "submitted" message without the page reloading.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Form submitted
    echo "submitted";
    exit; // Stop further execution
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Form Submission</title>
</head>
<body>
    <form id="myForm" method="post">
        <!-- Form fields here -->
        <input type="submit" name="submit" value="weiter">
    </form>

    <script>
        document.getElementById("myForm").addEventListener("submit", function(event) {
            event.preventDefault();
            var formData = new FormData(this);

            fetch('submit.php', {
                method: 'POST',
                body: formData
            })
            .then(response => response.text())
            .then(data => {
                console.log(data); // Output the response
            });
        });
    </script>
</body>
</html>