Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. <!--Includes HTML, Head, Scripts, CSS, Body and begin of Content div-->
  2. <?php include realpath(__DIR__)."/layout/header.php"; ?>
  3. <?php
  4. function Validate() {
  5. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  6. return IsValid("username") && IsValid("password") && IsValid("usermail");
  7. } else {
  8. return true;
  9. }
  10. }
  11. function IsValid($name) {
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  13. switch($name){
  14. case 'username':
  15. return !isset($_POST["username"]) || strlen($_POST["username"]) > 0;
  16. break;
  17. case 'password':
  18. return !isset($_POST["password"]) || strlen($_POST["password"]) > 0;
  19. break;
  20. case 'usermail':
  21. return strlen($_POST["usermail"]) > 0 && filter_var($_POST["usermail"], FILTER_VALIDATE_EMAIL);
  22. break;
  23. }
  24. } else {
  25. return true;
  26. }
  27. }
  28. function GetErrorMessage($name) {
  29. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  30. switch($name){
  31. case 'username':
  32. return !isset($_POST["username"]) || strlen($_POST["username"]) == 0 ? 'Username is required' : '';
  33. break;
  34. case 'password':
  35. return !isset($_POST["username"]) || strlen($_POST["password"]) == 0 ? 'Password is required' : '';
  36. break;
  37. case 'usermail':
  38. $message = '';
  39. if (strlen($_POST["usermail"]) == 0) {
  40. $message = $message.'E-mail is required';
  41. }
  42. if(strlen($_POST["usermail"]) != 0 && !filter_var($_POST["usermail"], FILTER_VALIDATE_EMAIL)) {
  43. if(strlen($message) > 0) {
  44. $message = $message.'<br />';
  45. }
  46. $message = $message.'"'.$_POST["usermail"].'" is not a valid e-mail address';
  47. }
  48. return $message;
  49. break;
  50. }
  51. } else {
  52. return '';
  53. }
  54. }
  55. ?>
  56. <div class="row">
  57. <div class="col-md-4">
  58. <form action="./userform.php" method="POST" role="form">
  59. <div class="form-group <?php echo IsValid('username') ? '' : 'has-error' ?>">
  60. <label for="username">Username</label>
  61. <input type="text" id="username" name="username" class="form-control" value="<?php echo Validate() ? '' : $_POST["username"] ?>" />
  62. <span class="help-block"><?php echo GetErrorMessage('username') ?></span>
  63. </div>
  64. <div class="form-group <?php echo IsValid('password') ? '' : 'has-error' ?>">
  65. <label for="password">Password</label>
  66. <input type="password" id="password" name="password" class="form-control" value="<?php echo Validate() ? '' : $_POST["password"] ?>" />
  67. <span class="help-block"><?php echo GetErrorMessage('password') ?></span>
  68. </div>
  69. <div class="form-group <?php echo IsValid('usermail') ? '' : 'has-error' ?>">
  70. <label for="usermail">E-Mail</label>
  71. <input type="text" id="usermail" name="usermail" class="form-control" value="<?php echo Validate() ? '' : $_POST["usermail"] ?>" />
  72. <span class="help-block"><?php echo GetErrorMessage('usermail') ?></span>
  73. </div>
  74. <div class="form-group">
  75. <button type="submit" class="btn btn-default">Submit</button>
  76. </div>
  77. </form>
  78. </div>
  79. <?php
  80. if ($_SERVER['REQUEST_METHOD'] === 'POST' && Validate()) {
  81. echo '<div class="col-md-4"><h4>Data from POST</h4><p>Username: '.$_POST["username"].'</p><p>Password: '.$_POST["password"].'</p><p>E-Mail: '.$_POST["usermail"].'</p></div>';
  82. } else if($_SERVER['REQUEST_METHOD'] === 'POST' && !Validate()) {
  83. echo '<div class="col-md-4"><h4>Data from POST is not valid</h4></div>';
  84. }
  85. ?>
  86. </div>
  87. <!--Includes End of Content div, Body and HTML-->
  88. <?php include realpath(__DIR__)."/layout/footer.php"; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement