karlakmkj

Simple form validation

Sep 20th, 2021
817
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Define checkWord() here:
  3.   function checkWord($input, $letter){
  4.       if ($_SERVER["REQUEST_METHOD"] === "POST" && strtolower($input[0]) != $letter){  //also to check same letter despite whether upper or lower case
  5.         return "* This word must start with the letter $letter!";
  6.       }
  7.       else {
  8.         return "";
  9.       }
  10.   }
  11.  
  12. ?>
  13.  
  14. <h1>Time to Practice our ABCs</h1>
  15. <form method="post" action="">
  16.     Enter a word that starts with the letter "a":
  17.     <br>
  18.     <!-- Assign the value attribute of each input tag to the value submitted by a user. User should be able to see what they entered in input field once they submit their form.  -->
  19.     <input type="text" name="a-word" id="a-word" value= "<?= $_POST["a-word"]; ?>">
  20.     <br>      
  21.     <!-- Invoke the function here to pass user's input into function for checking -->
  22.       <p class="error" id="a-error"><?= checkWord($_POST["a-word"], "a"); ?></p>
  23.     <br>    
  24.     Enter a word that starts with the letter "b":
  25.     <br>
  26.     <input type="text" id="b-word" name="b-word" value= "<?= $_POST["b-word"]; ?>">
  27.     <br>      
  28.       <p class="error" id="b-error"><?= checkWord($_POST["b-word"], "b"); ?></p>
  29.     <br>
  30.     Enter a word that starts with the letter "c":
  31.     <br>
  32.     <input type="text" id="c-word" name="c-word" value= "<?= $_POST["c-word"]; ?>">
  33.     <br>      
  34.       <p class="error" id="c-error"><?= checkWord($_POST["c-word"], "c"); ?></p>
  35.     <br>
  36.     <input type="submit" value="Submit Words">
  37. </form>
  38. <div>
  39.     <h3>"a" is for: <?= $_POST["a-word"];?><h3> <!-- What user entered can be seen here -->
  40.     <h3>"b" is for: <?= $_POST["b-word"];?><h3>
  41.     <h3>"c" is for: <?= $_POST["c-word"];?><h3>    
  42. <div>  
RAW Paste Data