How can JavaScript be used to intercept and handle Carriage Return (CR) inputs in PHP forms?

When a user enters a Carriage Return (CR) in a PHP form, it can cause unexpected behavior or errors. To handle this, you can use JavaScript to intercept the CR input and prevent it from being submitted with the form. This can be done by capturing the key press event for the Enter key and preventing its default behavior.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $input = $_POST['input'];
    
    // Handle the input data here
}

?>

<form method="post">
    <input type="text" name="input" id="input">
    <button type="submit">Submit</button>
</form>

<script>
document.getElementById('input').addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        e.preventDefault();
        // You can also display a message to the user here
    }
});
</script>