How can PHP scripts be used to handle form data without redirecting the calling page?

When handling form data in PHP without redirecting the calling page, you can use AJAX to send the form data to a PHP script in the background. The PHP script can process the form data and return a response without reloading the page. This allows for a seamless user experience without any page redirection.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    $name = $_POST['name'];
    $email = $_POST['email'];

    // Perform any necessary validation or database operations

    // Return a response
    $response = "Hello, " . $name . "! Your email is: " . $email;
    echo $response;
    exit;
}
?>