duanren9163 2018-04-14 20: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

        //Check if these fields have data in them
    if(isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirm']) || isset($_POST['username'])   ){
        if(!$_POST['email'] || !$_POST['password']  || !$_POST['confirm'] || !$_POST['username']   ){
            $error = "Error. Please write in all required fields with the * symbol";
        }

        // Validate email format
        //https://www.w3schools.com/php/php_form_url_email.asp
        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        } else {
            $error = "Error. Please write in the correct E-Mail format";
        }

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

    <?php

    //Check if these fields have data in them
    if(isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirm']) || isset($_POST['username'])   ){
        if(!$_POST['email'] || !$_POST['password']  || !$_POST['confirm'] || !$_POST['username']   ){
            $error = "Error. Please write in all required fields with the * symbol";
        }

        // Validate email format
        //https://www.w3schools.com/php/php_form_url_email.asp
        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        } else {
            $error = "Error. Please write in the correct E-Mail format";
        }


        if(!$error){
            //No errors - let’s create the account
            //Encrypt the password with a salt
            $encryptedPass = password_hash($_POST['password'], PASSWORD_DEFAULT);
            //Insert DB
            $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)";
            $result = $DBH->prepare($query);
            $result->bindParam(':username', $_POST['username']);
            $result->bindParam(':firstname', $_POST['firstname']);
            $result->bindParam(':lastname', $_POST['lastname']);
            $result->bindParam(':email', $_POST['email']);
            $result->bindParam(':gender', $_POST['gender']);
            $result->bindParam(':country', $_POST['country']);
            $result->bindParam(':mobile', $_POST['mobile']);
            $result->bindParam(':password', $encryptedPass);

            if($result->execute()){
                echo '<div class="alert alert-success" role="alert">Registration Successful!</div>';
                echo "<script> window.location.assign('index.php?p=login'); </script>";
            }
        }

HTML

<div class="row">
        <div class="main-login main-center">
            <h1>Register</h1>
            <form action="index.php?p=register" method="post">

                <?php if($error){
                    echo '<div class="alert alert-danger" role="alert">
                    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                    <span class="sr-only">Error:</span>
                    '.$error.'
                    </div>';
                } ?>

                <p>
                    * Required Fields
                </p>

                <div class="form-group">
                    <label for="email" class="cols-sm-2 control-label">* Username</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="username" id="username"  placeholder="Username"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="email" class="cols-sm-2 control-label">* E-Mail Address</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="email" id="email"  placeholder="example@hotmail.com"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="password" class="cols-sm-2 control-label">* Password</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
                            <input type="password" class="form-control" name="password" id="password"  placeholder="Password"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="confirm" class="cols-sm-2 control-label">* Confirm Password</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
                            <input type="password" class="form-control" name="confirm" id="confirm"  placeholder="Confirm Password"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="email" class="cols-sm-2 control-label">First Name</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user-circle fa" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="firstname" id="firstname"  placeholder="First Name"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="email" class="cols-sm-2 control-label">Last Name</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user-circle fa" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="lastname" id="lastname"  placeholder="Last Name"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    Gender:
                    <div class="btn-group btn-group-toggle" data-toggle="buttons">
                        <label class="btn btn-outline-info active">
                            <!-- This button is checked by default for the user  -->
                            <input type="radio" name="gender" id="gender_male" value="Male" autocomplete="off" checked=""> Male
                        </label>

                        <label class="btn btn-outline-info">
                            <input type="radio" name="gender" id="gender_female"  value="Female" autocomplete="off"> Female
                        </label>
                    </div>
                </div>

                <div class="form-group">
                    <label for="address" class="cols-sm-2 control-label">Country</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-address-card" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" id="placesSearch" placeholder="Country" name="country">
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="mobile" class="cols-sm-2 control-label">Mobile Number</label>
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-phone fa-lg" aria-hidden="true"></i></span>
                            <input type="mobile" class="form-control" name="mobile" id="mobile"  placeholder="Mobile Number"/>
                        </div>
                    </div>
                </div>

                <div align="center" class="checkbox">
                    <label><input type="checkbox" value=""> Sign up for BakeryUp Newsletters!</label>
                </div>

                <div class="form-group ">
                    <button type="submit" class="btn btn-success btn-lg btn-block login-button">Join</button>
                </div>
                <div align="center">
                    By clicking 'Join' you are agreeing to our Terms and Conditions, Cookie Policy and Privacy Policy.
                </div>
            </form>
        </div>
    </div>
  • 写回答

2条回答 默认 最新

  • dounan4479 2018-04-14 20: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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效