MaxxxxiorS

PHP validation form

Nov 10th, 2021
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.02 KB | None | 0 0
  1. <?php
  2. // define variables and set to empty values
  3. $imieErr = $nazwiskoErr = $wiekErr = $adresEmailErr = $hasloErr = $hasloRepeatErr = "";
  4. $imie = $nazwisko = $wiek = $adresEmail = $haslo = $hasloRepeat = "";
  5.  
  6. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  7. //imie
  8.   if (empty($_POST["imie"])) {
  9.     $imieErr = "imie is required";
  10.   } else {
  11.     $imie = test_input($_POST["imie"]);
  12.     // check if imie only contains letters and whitespace
  13.     if (!preg_match("/^[a-zA-Z-' ]*$/",$imie)) {
  14.       $imieErr = "Only letters and white space allowed";
  15.     }
  16.   }
  17. //nazwisko
  18.   if (empty($_POST["nazwisko"])) {
  19.     $nazwiskoErr = "nazwisko is required";
  20.   } else {
  21.     $nazwisko = test_input($_POST["nazwisko"]);
  22.     // check if nazwisko only contains letters and whitespace
  23.     if (!preg_match("/^[a-zA-Z-' ]*$/",$nazwisko)) {
  24.       $nazwiskoErr = "Only letters and white space allowed";
  25.     }
  26.   }
  27. //wiek
  28.   if (empty($_POST["wiek"])) {
  29.       $wiekErr = "wiek is required";
  30.   } else {
  31.       $wiek = test_input($_POST["wiek"]);
  32.       // check if wiek only contains numbers
  33.       if (!is_numeric($wiek)) {
  34.           $wiekErr = "wiek is not a number";
  35.       }
  36.   }
  37. //adresEmail
  38.   if (empty($_POST["adresEmail"])) {
  39.     $adresEmailErr = "adresEmail is required";
  40.   } else {
  41.     $adresEmail = test_input($_POST["adresEmail"]);
  42.     // check if e-mail address is well-formed
  43.     if (!filter_var($adresEmail, FILTER_VALIDATE_EMAIL)) {
  44.       $adresEmailErr = "Invalid adresEmail format";
  45.     }
  46.   }
  47. //haslo
  48.     if (strcmp($_POST["haslo"], $_POST["hasloRepeat"]) == 0) {
  49.         if (empty($_POST["haslo"])) {
  50.             $hasloErr = "haslo is required";
  51.         } else {
  52.             $haslo = test_input($_POST["haslo"]);
  53.         }
  54.  
  55.         if (empty($_POST["hasloRepeat"])) {
  56.             $hasloRepeatErr = "you need to repeat password";
  57.         } else {
  58.             $hasloRepeat = test_input($_POST["hasloRepeat"]);
  59.         }
  60.     } else {
  61.         $hasloErr = "Passwords must match!";
  62.         $hasloRepeatErr = "Passwords must match!";
  63.     }
  64. }
  65.  
  66. function test_input($data) {
  67.   $data = trim($data);
  68.   $data = stripslashes($data);
  69.   $data = htmlspecialchars($data);
  70.   return $data;
  71. }
  72. ?>
  73.  
  74. <style>
  75.     #lubiemBox {
  76.         justify-content: center;
  77.         align-items: center;
  78.     }
  79.  
  80.     .error {
  81.         color: red;
  82.     }
  83. </style>
  84.  
  85. <h2>Formularz fajny</h2>
  86. <p><span class="error">* wymagane pole</span></p>
  87. <form method="post" action="">  
  88. <!-- imie -->
  89.   Imię: <input type="text" name="imie" placeholder="Jan" value="<?php echo $imie;?>">
  90.   <span class="error">* <?php echo $imieErr;?></span>
  91.   <br><br>
  92. <!-- nazwisko -->
  93.   Nazwisko: <input type="text" name="nazwisko" placeholder="Kowalski" value="<?php echo $nazwisko;?>">
  94.   <span class="error">* <?php echo $nazwiskoErr;?></span>
  95.   <br><br>
  96. <!-- wiek -->
  97.   Wiek: <input type="text" name="wiek" placeholder="18" value="<?php echo $wiek;?>">
  98.   <span class="error">* <?php echo $wiekErr;?></span>
  99.   <br><br>
  100. <!-- adresEmail -->
  101.   E-mail: <input type="text" name="adresEmail" placeholder="[email protected]" value="<?php echo $adresEmail;?>">
  102.   <span class="error">* <?php echo $adresEmailErr;?></span>
  103.   <br><br>
  104. <!-- haslo -->
  105.   Hasło: <input type="password" name="haslo" placeholder="**********" value="<?php echo $hasloEmail;?>">
  106.   <span class="error">* <?php echo $hasloErr;?></span>
  107.   <br><br>
  108.   Powtórz hasło: <input type="password" name="hasloRepeat" placeholder="**********" value="<?php echo $hasloRepeatEmail;?>">
  109.   <span class="error">* <?php echo $hasloRepeatErr;?></span>
  110.   <br><br>
  111. <!-- checkbox -->
  112.     <div id="lubiemBox">
  113.         Co lubisz: <br>
  114.         <input type="checkbox" name="lubiem[]" id="lubiem" value="Placki"> Placki<br>
  115.         <input type="checkbox" name="lubiem[]" id="lubiem" value="Kasztany"> Kasztany<br>
  116.         <input type="checkbox" name="lubiem[]" id="lubiem" value="Pyry"> Pyry<br>
  117.         <input type="checkbox" name="lubiem[]" id="lubiem" value="Kartofelki"> Kartofelki
  118.         <br><br>
  119.     </div>
  120. <!-- radio -->
  121.     <div id="nieLubiemBox">
  122.         Czego nie lubisz: <br>
  123.         <input type="radio" name="nieLubiem" id="nieLubiem" value="Ananas na picy"> Ananas na picy<br>
  124.         <input type="radio" name="nieLubiem" id="nieLubiem" value="Brokuly"> Brokułów<br>
  125.         <input type="radio" name="nieLubiem" id="nieLubiem" value="Bruksela"> Brukseli<br>
  126.         <br><br>
  127.     </div>
  128. <!-- submit -->
  129.     <input type="submit" name="submit" value="Submit">
  130. </form>
  131.  
  132. <?php
  133. echo "<h2>Your Input:</h2>";
  134. echo $imie;
  135. echo "<br>";
  136. echo $nazwisko;
  137. echo "<br>";
  138. echo $wiek;
  139. echo "<br>";
  140. echo $adresEmail;
  141. echo "<br>";
  142. echo $haslo;
  143. echo "<br>";
  144.  
  145. $fajenowix = $_POST['lubiem'];
  146. if(isset($_POST['lubiem'])) {
  147.     echo "Lubisz: ";
  148.     foreach ($fajenowix as $elomix) {
  149.         echo $elomix.", ";
  150.     }
  151.    
  152. } else {
  153.     echo "Nie wybrałeś co lubisz.";
  154. }
  155.  
  156. echo "<br>";
  157. $siemanix = $_POST['nieLubiem'];
  158. if(isset($_POST['nieLubiem'])) {
  159.     echo "Nie lubisz: ";
  160.     echo $_POST['nieLubiem'];
  161. } else {
  162.     echo "Nie wybrałeś czego nie lubisz.";
  163. }
  164.  
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment