Tags
Asked 3 years ago
9 Jun 2021
Views 317
kiran

kiran posted

Form does not submit with php echo

Form does not submit with php echo
jignesh

jignesh
answered Apr 26 '23 00:00

When using PHP echo in a form, it's important to make sure that the echo statement is enclosed within the appropriate HTML form tags. Here is an example of how to properly use echo in a form:



<!DOCTYPE html>
<html>
<head>
    <title>Form with PHP Echo</title>
</head>
<body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name">

        <br><br>

        <label for="email">Email:</label>
        <input type="email" name="email" id="email">

        <br><br>

        <?php
            // Check if form is submitted
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                // Retrieve form data
                $name = $_POST["name"];
                $email = $_POST["email"];
                // Display form data
                echo "<p>Your name is $name</p>";
                echo "<p>Your email is $email</p>";
            }
        ?>

        <input type="submit" value="Submit">
    </form>
</body>
</html>

In this example, the PHP echo statements are enclosed within the PHP code block, and the form's action attribute is set to <?php echo $_SERVER['PHP_SELF']; ?> to ensure that the form data is submitted back to the same page. The form data is retrieved using the $_POST global variable, and the form data is then displayed using the echo statement within the PHP code block.
Post Answer