PHP form Validation Example
In this example, we have discus how to use PHP Form Validation. First off all we have displayed HTML form with text fields and submit button. In this example we have used method and function. First thing we had done is to pass all variables through PHP's htmlspecalchars() function. After the we fills all input field than ,we click submit button and display all input fields values. Now we have used $_SERVER["REQUEST_METHOD"]. If the REQUEST_METHOD is POST, and submitted form. If user not submit input fields skip the validation and display a blank form.
Example of PHP Validation:
In this example, we have discus how to use PHP Form Validation. First off all we have displayed HTML form with text fields and submit button. In this example we have used method and function. First thing we had done is to pass all variables through PHP's htmlspecalchars() function. After the we fills all input field than ,we click submit button and display all input fields values. Now we have used $_SERVER["REQUEST_METHOD"]. If the REQUEST_METHOD is POST, and submitted form. If user not submit input fields skip the validation and display a blank form.
Example of PHP Validation:
<!DOCTYPE HTML> <html> <head> </head> <body> <?php $name = $email = $gender = $address = $mobile = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $mobile = test_input($_POST["mobile"]); $address = test_input($_POST["address"]); $gender = test_input($_POST["gender"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP Form Validation</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <table llpadding="2" width="20%" bgcolor="99FFFF" cellspacing="2" border="1"> <tr> <td>Name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>E-mail:</td> <td><input type="text" name="email"></td> </tr> <tr> <td>Moblie No:</td> <td><input type="text" name="mobile"></td> </tr> <tr> <td>Address</td> <td> <textarea name="address" rows="5" cols="20"></textarea></td> </tr> <tr> <td>Gender:</td> <td> <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male</td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> </table> </form> <?php echo "<h2><Your Input:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $mobile; echo "<br>"; echo $address; echo "<br>"; echo $gender; ?> </body> </html>Output
Great beginning php tutorials Very clear and helpful for beginners.
ReplyDelete