How can JavaScript be integrated with PHP to improve user experience when validating form inputs?

When validating form inputs, integrating JavaScript with PHP can improve the user experience by providing instant feedback to users without needing to reload the page. This can help users quickly identify and correct any errors in their input before submitting the form.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];

    // Server-side validation
    if (empty($name) || empty($email)) {
        echo "Please fill out all fields.";
    } else {
        // Additional validation logic here

        // If all validation passes, proceed with form submission
        echo "Form submitted successfully!";
    }
}
?>