/form_valiadiation_with_require.php

https://gitlab.com/Garincha/PHP_Fundametals · PHP · 119 lines · 101 code · 18 blank · 0 comment · 10 complexity · ca3ba62155f244fe5ee4ab1b7b8377fe MD5 · raw file

  1. <?php
  2. $errName = $errMail = $errWeb = $errGen = $errSub = $errCom = "";//these all are blank variable
  3. $Name = $Mail = $Web = $Com = $Gen = $Sub = "";//these all are blank variable
  4. if($_SERVER["REQUEST_METHOD"] == "POST"){//here, by this code we are ensuring that all request/data comes through post method.
  5. if(empty($_POST['user_name'])){
  6. $errName = "<span style='color:red'>Name is required</span>";
  7. } else {
  8. $Name = validate($_POST['user_name']);//getting the data through the key user_name and kepping in $name variable.
  9. }
  10. if(empty($_POST['user_email'])){
  11. $errMail = "<span style='color:red'>Email is required</span>";
  12. } else {
  13. $Mail = validate($_POST['user_email']);//getting the data through the key user_email and kepping in $mail variable.
  14. }
  15. if(empty($_POST['website'])){
  16. $errWeb = "<span style='color:red'>Website is required</span>";
  17. } else {
  18. $Web = validate($_POST['website']);//getting the data through the key website and kepping in $web variable.
  19. }
  20. $Com = validate($_POST['comment']);//getting the data through the key comment and kepping in $com variable.
  21. if(empty($_POST['gender'])){
  22. $errGen = "<span style='color:red'>Gender is required</span>";
  23. } else {
  24. $Gen = validate($_POST['gender']);//getting the data through the key gender and kepping in $gen variable.
  25. }
  26. $Sub = validate($_POST['submit']);//getting the data through the key submit and kepping in $sub variable.
  27. echo "Name : ".$Name."<br>";
  28. echo "Mail : ".$Mail."<br>";
  29. echo "Website : ".$Web."<br>";
  30. echo "Comment : ".$Com."<br>";
  31. echo "Gender : ".$Gen."<br>";
  32. echo "Submit : ".$Sub;
  33. }
  34. function validate($data){//data(exe-$_post(user_name)) first comes to this parameter $data.by this function we are validating our data.
  35. $data1 = trim($data);//then it goes through trim function and stores in $data1.
  36. $data2 = stripcslashes($data1);//then it goes through stripclashed function and stores in $data2
  37. $data3 = htmlspecialchars($data2);//then it goes through htmlspecialchars function and stores in $data3
  38. return $data3;
  39. }
  40. ?>
  41. <html>
  42. <head>
  43. <title>PHP Syntax</title>
  44. <style>
  45. .code{width:900px; margin: 0 auto; background:<?php echo '#ddd';?>;text-align: center;}
  46. .headeroption,.footeroptions{background: #444;color: #fff;text-align: center;padding: 20px;}
  47. .maincontent{min-height: 300px;padding: 20px;}
  48. .headeroption h2,.footeroptions h2{margin: 0;}
  49. </style>
  50. </head>
  51. <body>
  52. <div class="code">
  53. <div class="headeroption">
  54. <h2><?php echo 'PHP Fundamentals'?></h2>
  55. </div>
  56. <div class="maincontent">
  57. <?php
  58. echo 'PHP Form Validation';
  59. ?>
  60. <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);//in action we are using this code to take action within this page or we can leave the place empty.to protect hacking we are using htmlspecialchars?>">
  61. <table>
  62. <p style="color:red">* Required Field</p>
  63. <tr>
  64. <td>Name :</td>
  65. <td><input type="text" name="user_name" >*<?php echo "$errName"?></td>
  66. </tr>
  67. <tr>
  68. <td>Email :</td>
  69. <td><input type="text" name="user_email" >*<?php echo "$errMail"?></td>
  70. </tr>
  71. <tr>
  72. <td>Website :</td>
  73. <td><input type="text" name="website" >*<?php echo "$errWeb"?></td>
  74. </tr>
  75. <tr>
  76. <td>Comments :</td>
  77. <td><textarea name="comment" rows="5" cols="50"></textarea></td>
  78. </tr>
  79. <tr>
  80. <td>Gender :</td>
  81. <td>
  82. <input type="radio" name="gender" value="male">Male
  83. <input type="radio" name="gender" value="female">female
  84. *<?php echo "$errGen"?>
  85. </td>
  86. </tr>
  87. <tr>
  88. <td></td>
  89. <td><input type="submit" name="submit" value="Submit"></td>
  90. </tr>
  91. </table>
  92. </form>
  93. <?php
  94. ?>
  95. </div>
  96. <div class="footeroptions">
  97. <h2><?php echo 'www.w3schools.com';?></h2>
  98. </div>
  99. </div>
  100. </body>
  101. </html>