Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1.  
  2. <?php
  3. // Initializing Error Variables To Null.
  4. $nameError ="";
  5. $emailError ="";
  6. $websiteError ="";
  7. // This code block will execute when form is submitted
  8. if(isset($_POST['submit'])){
  9. /*--------------------------------------------------------------
  10. Fetch name value from URL and Sanitize it
  11. --------------------------------------------------------------*/
  12. if($_POST['name'] != ""){
  13. // Sanitizing name value of type string
  14. $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
  15. $nameError = "<span class='valid'>".$_POST['name']. "</span>is Sanitized an Valid name.";
  16. if ($_POST['name'] == ""){
  17. $nameError = "<span class='invalid'>Please enter a valid name.</span>";
  18. }
  19. }
  20. else {
  21. $nameError = "<span class='invalid'>Please enter your name.</span>";
  22. }
  23. /*------------------------------------------------------------------
  24. Fetch email value from URL, Sanitize and Validate it
  25. --------------------------------------------------------------------*/
  26. if($_POST['email'] != ""){
  27. //sanitizing email
  28. $_POST['email'] = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
  29. //After sanitization Validation is performed
  30. $_POST['email'] = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
  31. $emailError = "<span class='valid'>".$_POST['email']." </span>is Sanitized an Valid Email.";
  32. if($_POST['email'] == ""){
  33. $emailError = "<span class='invalid'>Please enter a valid email.</span>";
  34. }
  35. }
  36. else {
  37. $emailError = "<span class='invalid'>Please enter your email.</span>";
  38. }
  39. /*---------------------------------------------------------------------------
  40. Fetch website value from URL, Sanitize and Validate it
  41. ----------------------------------------------------------------------------*/
  42. if($_POST['website'] != ""){
  43. //sanitizing URL
  44. $_POST['website'] = filter_var($_POST['website'], FILTER_SANITIZE_URL);
  45. //After sanitization Validation is performed
  46. $_POST['website'] = filter_var($_POST['website'], FILTER_VALIDATE_URL);
  47. $websiteError = "<span class='valid'>".$_POST['website']." </span>is Sanitized an Valid Website URL.";
  48. if ($_POST['website'] == ""){
  49. $websiteError = "<span class='invalid'>Please enter a valid website start with http:// </span>";
  50. }
  51. }
  52. else {
  53. $websiteError = "<span class='invalid'>Please enter your website URL.</span>";
  54. }
  55. }
  56. ?>
  57.  
  58. <!DOCTYPE html>
  59. <html>
  60. <head>
  61. <title>Form Sanitization and Validation Using PHP - Demo Preview</title>
  62. <meta content="noindex, nofollow" name="robots">
  63. <link href="style.css" rel="stylesheet">
  64. </head>
  65. <body>
  66. <div class="maindiv">
  67. <div class="form_div">
  68. <div class="title">
  69. <h2>Form Sanitization and Validation Using PHP</h2>
  70. </div>
  71. <form action="" method="post">
  72. <h2>Form</h2>
  73. <p>* Required Fields</p>
  74. Name: <span class='invalid'>*</span>
  75. <input class="input" name="name" type="text" value="">
  76. <p><?php echo $nameError;?></p>
  77. E-mail: <span class='invalid'>*</span>
  78. <input class="input" name="email" type="text" value="">
  79. <p><?php echo $emailError;?></p>
  80. Website: <span class='invalid'>*</span>
  81. <input class="input" name="website" type="text" value="">
  82. <p><?php echo $websiteError;?></p><input class="submit" name="submit" type="submit" value="Submit">
  83. </form>
  84. </div>
  85. </div> <!-- HTML Ends Here -->
  86. </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement