Advertisement
wildanfuady

form_validasi.php

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