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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化