Skip to content
PHP Logo

How can PHP be utilized to process form data and display the results within the same page without opening a new window?

To process form data and display the results within the same page without opening a new window, you can use PHP to check if the form has been submitted, process the data, and then display the results on the same page. This can be achieved by using conditional statements to determine if the form has been submitted, and then echoing the results within the HTML code.

php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    $name = $_POST["name"];
    $email = $_POST["email"];
    
    // Display results
    echo "Thank you for submitting the form, $name. Your email address is $email.";
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="name">Name:</label>
    <input type="text" name="name" id="name">
    
    <label for="email">Email:</label>
    <input type="email" name="email" id="email">
    
    <input type="submit" value="Submit">
</form>