But PHP can also do input validation as the code below suggests.
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP Form Validation</title> </head> <body> <form action="valid_handler.php" method="POST"> <fieldset> <legend>Enter a quantity and email address</legend> <p>Quantity : <input type="text" name="quantity"></p> <p>Email Address : <input type="text" name="email"></p> </fieldset> <p><input type="submit" ></p> </form> </body> </html>The same (sort of) thing in JavaScript is:
<!DOCTYPE html> <html> <body> <h1>My First JavaScript</h1> <p>Please input a number.</p> <input id="demo" type="text"> <script> function myFunction() { var x=document.getElementById("demo").value; if(x==""||isNaN(x)) { alert("Not Numeric"); } } </script> <button type="button" onclick="myFunction()">Click Me!</button> </body> </html>
Without the benefit of other, more informed PHP-JavaScription opinions, I opinion is JavaScript is better to attempt this than PHP. Why? Because as a general rule, we prefer to do input validation as close to the source as possible. Rather than waiting for the HTML to get to the server, test it in the browser, save Internet latency, and conserve server cycles for when they are really needed.
No comments:
Post a Comment