How can the PHP code be modified to allow submission by pressing the Enter key instead of clicking the "Login" button?
To allow submission by pressing the Enter key instead of clicking the "Login" button in a PHP form, you can use JavaScript to trigger the form submission when the Enter key is pressed. This can be achieved by adding an event listener to the input fields that listens for the Enter key press event.
<form method="post" action="login.php" id="loginForm">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="button" onclick="login()">Login</button>
</form>
<script>
document.getElementById("loginForm").addEventListener("keyup", function(event) {
if (event.key === "Enter") {
document.getElementById("loginForm").submit();
}
});
function login() {
document.getElementById("loginForm").submit();
}
</script>
Keywords
Related Questions
- What are the best practices for handling user input and form data in PHP to prevent SQL injection attacks?
- How can PHP scripts be optimized for handling larger files and increasing script runtime for compression tasks?
- In PHP, what strategies can be employed to prevent bias and ensure equal opportunities for all participants in a contest with instant prizes?