Advertisement
Guest User

Untitled

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