Advertisement
karlakmkj

Basic validation with filter_var()

Sep 20th, 2021
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. <?php
  2. $validation_error = "";
  3. $user_url = "";
  4. $form_message = "";
  5.  
  6. if ($_SERVER["REQUEST_METHOD"] === "POST"){
  7.   $user_url = $_POST["url"];
  8.   if (!filter_var($user_url, FILTER_VALIDATE_URL)){  // if url is not valid
  9. $validation_error = "* This is an invalid URL.";
  10. $form_message = "Please retry and submit your form again.";
  11.   } else {
  12.     $form_message = "Thank you for your submission.";
  13.   }
  14. }
  15. ?>
  16.  
  17. <form method="post" action="">
  18. Your Favorite Website:
  19. <br>
  20. <input type="text" name="url" value="<?php echo $user_url;?>"> <!-- Valid URL must start with http:// -->
  21. <span class="error"><?= $validation_error;?></span>
  22. <br>
  23. <input type="submit" value="Submit">
  24. </form>
  25. <p> <?= $form_message;?> </p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement