How can CSS be used to visually indicate errors in form input fields in PHP?

To visually indicate errors in form input fields in PHP, you can use CSS to style the input fields with a red border or background color when an error occurs. This can help users easily identify where the error is in the form.

<?php
$error = ""; // Variable to hold error message

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate form input
    if (empty($_POST["input_field"])) {
        $error = "Please enter a value";
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <style>
        .error {
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <input type="text" name="input_field" class="<?php if(!empty($error)) { echo 'error'; } ?>">
        <span><?php echo $error; ?></span>
        <input type="submit" value="Submit">
    </form>
</body>
</html>