PHP Form Validation-Result Size: 705 x 631


SUBMITTED BY: henry1874w

DATE: June 17, 2017, 1:22 p.m.

FORMAT: Text only

SIZE: 3.0 kB

HITS: 193

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <style>
  5. .error {color: #FF0000;}
  6. </style>
  7. </head>
  8. <body>
  9. <?php
  10. // define variables and set to empty values
  11. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  12. $name = $email = $gender = $comment = $website = "";
  13. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  14. if (empty($_POST["name"])) {
  15. $nameErr = "Name is required";
  16. } else {
  17. $name = test_input($_POST["name"]);
  18. // check if name only contains letters and whitespace
  19. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  20. $nameErr = "Only letters and white space allowed";
  21. }
  22. }
  23. if (empty($_POST["email"])) {
  24. $emailErr = "Email is required";
  25. } else {
  26. $email = test_input($_POST["email"]);
  27. // check if e-mail address is well-formed
  28. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  29. $emailErr = "Invalid email format";
  30. }
  31. }
  32. if (empty($_POST["website"])) {
  33. $website = "";
  34. } else {
  35. $website = test_input($_POST["website"]);
  36. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  37. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  38. $websiteErr = "Invalid URL";
  39. }
  40. }
  41. if (empty($_POST["comment"])) {
  42. $comment = "";
  43. } else {
  44. $comment = test_input($_POST["comment"]);
  45. }
  46. if (empty($_POST["gender"])) {
  47. $genderErr = "Gender is required";
  48. } else {
  49. $gender = test_input($_POST["gender"]);
  50. }
  51. }
  52. function test_input($data) {
  53. $data = trim($data);
  54. $data = stripslashes($data);
  55. $data = htmlspecialchars($data);
  56. return $data;
  57. }
  58. ?>
  59. <h2>PHP Form Validation Example</h2>
  60. <p><span class="error">* required field.</span></p>
  61. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  62. Name: <input type="text" name="name" value="<?php echo $name;?>">
  63. <span class="error">* <?php echo $nameErr;?></span>
  64. <br><br>
  65. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  66. <span class="error">* <?php echo $emailErr;?></span>
  67. <br><br>
  68. Website: <input type="text" name="website" value="<?php echo $website;?>">
  69. <span class="error"><?php echo $websiteErr;?></span>
  70. <br><br>
  71. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  72. <br><br>
  73. Gender:
  74. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  75. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  76. <span class="error">* <?php echo $genderErr;?></span>
  77. <br><br>
  78. <input type="submit" name="submit" value="Submit">
  79. </form>
  80. <?php
  81. echo "<h2>Your Input:</h2>";
  82. echo $name;
  83. echo "<br>";
  84. echo $email;
  85. echo "<br>";
  86. echo $website;
  87. echo "<br>";
  88. echo $comment;
  89. echo "<br>";
  90. echo $gender;
  91. ?>
  92. </body>
  93. </html>

comments powered by Disqus