duanren9163 2018-04-14 12:34
浏览 43
已采纳

PHP表单验证重叠

I have an issue with php form validation, it works fine but it does not work as expected, right now I have two types of php form validation in use, one is to check if four fields have data in them and the other is to check if the right format is used for the E-Mail field.

The problem I have is that if I don't do any of the fields and click submit, I expect the "do these four fields" error message to pop up, but instead the E-mail validation does, if I do the e-mail fine then the required field message pops which is not what I want.

Code, I am aware that for the validate email, the if statement has no code in it, I don't really know what to put there

  1. //Check if these fields have data in them
  2. if(isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirm']) || isset($_POST['username']) ){
  3. if(!$_POST['email'] || !$_POST['password'] || !$_POST['confirm'] || !$_POST['username'] ){
  4. $error = "Error. Please write in all required fields with the * symbol";
  5. }
  6. // Validate email format
  7. //https://www.w3schools.com/php/php_form_url_email.asp
  8. if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  9. } else {
  10. $error = "Error. Please write in the correct E-Mail format";
  11. }

EDIT just want to clarify, I tried moving the validate email code above the checking code but it keeps a constant error message when the user enters the form to write in all the required fields

Entire PHP

  1. <?php
  2. //Check if these fields have data in them
  3. if(isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirm']) || isset($_POST['username']) ){
  4. if(!$_POST['email'] || !$_POST['password'] || !$_POST['confirm'] || !$_POST['username'] ){
  5. $error = "Error. Please write in all required fields with the * symbol";
  6. }
  7. // Validate email format
  8. //https://www.w3schools.com/php/php_form_url_email.asp
  9. if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  10. } else {
  11. $error = "Error. Please write in the correct E-Mail format";
  12. }
  13. if(!$error){
  14. //No errors - let’s create the account
  15. //Encrypt the password with a salt
  16. $encryptedPass = password_hash($_POST['password'], PASSWORD_DEFAULT);
  17. //Insert DB
  18. $query = "INSERT INTO users (user_email, user_password, user_forename, user_lastname, user_name, user_gender, user_country, user_number) VALUES (:email, :password, :firstname, :lastname, :username, :gender, :country, :mobile)";
  19. $result = $DBH->prepare($query);
  20. $result->bindParam(':username', $_POST['username']);
  21. $result->bindParam(':firstname', $_POST['firstname']);
  22. $result->bindParam(':lastname', $_POST['lastname']);
  23. $result->bindParam(':email', $_POST['email']);
  24. $result->bindParam(':gender', $_POST['gender']);
  25. $result->bindParam(':country', $_POST['country']);
  26. $result->bindParam(':mobile', $_POST['mobile']);
  27. $result->bindParam(':password', $encryptedPass);
  28. if($result->execute()){
  29. echo '<div class="alert alert-success" role="alert">Registration Successful!</div>';
  30. echo "<script> window.location.assign('index.php?p=login'); </script>";
  31. }
  32. }

HTML

  1. <div class="row">
  2. <div class="main-login main-center">
  3. <h1>Register</h1>
  4. <form action="index.php?p=register" method="post">
  5. <?php if($error){
  6. echo '<div class="alert alert-danger" role="alert">
  7. <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  8. <span class="sr-only">Error:</span>
  9. '.$error.'
  10. </div>';
  11. } ?>
  12. <p>
  13. * Required Fields
  14. </p>
  15. <div class="form-group">
  16. <label for="email" class="cols-sm-2 control-label">* Username</label>
  17. <div class="cols-sm-10">
  18. <div class="input-group">
  19. <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
  20. <input type="text" class="form-control" name="username" id="username" placeholder="Username"/>
  21. </div>
  22. </div>
  23. </div>
  24. <div class="form-group">
  25. <label for="email" class="cols-sm-2 control-label">* E-Mail Address</label>
  26. <div class="cols-sm-10">
  27. <div class="input-group">
  28. <span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
  29. <input type="text" class="form-control" name="email" id="email" placeholder="example@hotmail.com"/>
  30. </div>
  31. </div>
  32. </div>
  33. <div class="form-group">
  34. <label for="password" class="cols-sm-2 control-label">* Password</label>
  35. <div class="cols-sm-10">
  36. <div class="input-group">
  37. <span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
  38. <input type="password" class="form-control" name="password" id="password" placeholder="Password"/>
  39. </div>
  40. </div>
  41. </div>
  42. <div class="form-group">
  43. <label for="confirm" class="cols-sm-2 control-label">* Confirm Password</label>
  44. <div class="cols-sm-10">
  45. <div class="input-group">
  46. <span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
  47. <input type="password" class="form-control" name="confirm" id="confirm" placeholder="Confirm Password"/>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label for="email" class="cols-sm-2 control-label">First Name</label>
  53. <div class="cols-sm-10">
  54. <div class="input-group">
  55. <span class="input-group-addon"><i class="fa fa-user-circle fa" aria-hidden="true"></i></span>
  56. <input type="text" class="form-control" name="firstname" id="firstname" placeholder="First Name"/>
  57. </div>
  58. </div>
  59. </div>
  60. <div class="form-group">
  61. <label for="email" class="cols-sm-2 control-label">Last Name</label>
  62. <div class="cols-sm-10">
  63. <div class="input-group">
  64. <span class="input-group-addon"><i class="fa fa-user-circle fa" aria-hidden="true"></i></span>
  65. <input type="text" class="form-control" name="lastname" id="lastname" placeholder="Last Name"/>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="form-group">
  70. Gender:
  71. <div class="btn-group btn-group-toggle" data-toggle="buttons">
  72. <label class="btn btn-outline-info active">
  73. <!-- This button is checked by default for the user -->
  74. <input type="radio" name="gender" id="gender_male" value="Male" autocomplete="off" checked=""> Male
  75. </label>
  76. <label class="btn btn-outline-info">
  77. <input type="radio" name="gender" id="gender_female" value="Female" autocomplete="off"> Female
  78. </label>
  79. </div>
  80. </div>
  81. <div class="form-group">
  82. <label for="address" class="cols-sm-2 control-label">Country</label>
  83. <div class="cols-sm-10">
  84. <div class="input-group">
  85. <span class="input-group-addon"><i class="fa fa-address-card" aria-hidden="true"></i></span>
  86. <input type="text" class="form-control" id="placesSearch" placeholder="Country" name="country">
  87. </div>
  88. </div>
  89. </div>
  90. <div class="form-group">
  91. <label for="mobile" class="cols-sm-2 control-label">Mobile Number</label>
  92. <div class="cols-sm-10">
  93. <div class="input-group">
  94. <span class="input-group-addon"><i class="fa fa-phone fa-lg" aria-hidden="true"></i></span>
  95. <input type="mobile" class="form-control" name="mobile" id="mobile" placeholder="Mobile Number"/>
  96. </div>
  97. </div>
  98. </div>
  99. <div align="center" class="checkbox">
  100. <label><input type="checkbox" value=""> Sign up for BakeryUp Newsletters!</label>
  101. </div>
  102. <div class="form-group ">
  103. <button type="submit" class="btn btn-success btn-lg btn-block login-button">Join</button>
  104. </div>
  105. <div align="center">
  106. By clicking 'Join' you are agreeing to our Terms and Conditions, Cookie Policy and Privacy Policy.
  107. </div>
  108. </form>
  109. </div>
  110. </div>

展开全部

  • 写回答

2条回答 默认 最新

  • dounan4479 2018-04-14 12:43
    关注

    Try this approach

    $required = ['email','password','confirm','username'];
    $countFieldFilled = 0;
    
    foreach($_POST as $key => $value) {
        if (!empty($value)) {
            $countFieldFilled++;
        }
    }
    
    $postedKeys = array_keys($_POST);
    
    if (count($_POST) > 0) {
        if ($countFieldFilled >= 4 && count(array_intersect($required,$postedKeys)) >= 4) {
            if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            //all ok !
            } else {
                $error = "Error. Please write in the correct E-Mail format";
            }
        } else {
            echo 'not all required fields entered';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部