How can the use of JavaScript alerts be improved in PHP form validation?

When using JavaScript alerts for form validation in PHP, it's important to provide more user-friendly messages and styling. Instead of using the default alert boxes, you can create custom alert messages that are styled to match your website's design. This can improve the overall user experience and make the validation process more visually appealing.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    
    if (empty($name)) {
        echo "<script>alert('Please enter your name');</script>";
    }
}
?>