What are the best practices for integrating a contact form in a separate window on a website using PHP?

When integrating a contact form in a separate window on a website using PHP, it is important to ensure that the form submission is handled correctly and securely. One common practice is to use AJAX to submit the form data asynchronously without reloading the entire page. This allows for a seamless user experience and also helps prevent spam submissions.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process the form submission
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    
    // Validate the form data
    // Implement your validation logic here
    
    // Send the email
    $to = "youremail@example.com";
    $subject = "Contact Form Submission";
    $headers = "From: $email";
    $body = "Name: $name\nEmail: $email\n\n$message";
    
    if (mail($to, $subject, $body, $headers)) {
        echo "Message sent successfully";
    } else {
        echo "Error sending message";
    }
}
?>