How can AJAX be used to handle form submission in PHP to avoid page reloads?

To handle form submission in PHP without page reloads, AJAX can be used to send form data asynchronously to the server and receive a response without refreshing the page. This allows for a more seamless user experience and can improve the performance of the website.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data here
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    // Return a response
    $response = "Hello, " . $name . "! Your email is: " . $email;
    
    // Send response back to the client
    echo $response;
}
?>