How can a PHP script be structured to handle form submissions from a popup window and close the window simultaneously?

To handle form submissions from a popup window and close the window simultaneously, you can use JavaScript to submit the form asynchronously to a PHP script. In the PHP script, process the form data and then return a response to the JavaScript function in the popup window to close it.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    // Return response to close the popup window
    echo "<script>window.opener.postMessage('Form submitted successfully', '*'); window.close();</script>";
}
?>