doutangguali32556 2016-10-28 03:47
浏览 34
已采纳

即使所有字段实际上都没有,php也显示'Empty'

In this form I created, it checks if the fields are set and are empty. I made a class with two methods, areSet() as isset(), and areEmpty() as empty(). In the areEmpty() method, it is supposed to display "Empty" if all the fields are empty, but even when all the fields are filled up, it still displays "Empty". Is it because of the implode() function that I used? I need someone's help to point out the wrong with my code.

HTML

<form action="validate.php" method="post">
    <input type="text" name="firstname" placeholder="First Name" autocomplete="off"><br>
    <input type="text" name="lastname" placeholder="Last Name" autocomplete="off"><br>
    <input type="text" name="email" placeholder="Email" autocomplete="off"><br>
    <input type="text" name="username" placeholder="Username" autocomplete="off"><br>
    <input type="password" name="password" placeholder="Password" autocomplete="off"><br>
    <input type="password" name="passwordAgain" placeholder="Confirm password" autocomplete="off"><br>
    <input type="submit" value="Submit">
</form>

validate.php

<?php

require 'include/class.php';

if(Input::areSet('firstname','lastname','email','username','password','passwordAgain')) {
    if(Input::areEmpty('firstname','lastname','email','username','password','passwordAgain')) {
        echo 'Empty';
    } else {
        echo 'Not empty';
    }
}

class.php

<?php

class Input {
    // isset()
    public static function areSet(...$fields) {
        $setFieldsArray = [];

        foreach($fields as $field) {
            $setFieldsArray[] = $_POST[$field];
        }

        return isset($setFieldsArray);
    }

    // empty()
    public static function areEmpty(...$fields) {
        $emptyFieldsArray = [];

        foreach($fields as $field) {
            $emptyFieldsArray[] = empty($_POST[$field]);
        }

        return implode(' || ', $emptyFieldsArray);
    }
}

enter image description here

  • 写回答

1条回答 默认 最新

  • dtzjvj3915 2016-10-28 03:56
    关注

    I have no idea what you're doing there by setting an array and then returning an imploded array; but just loop through the $fields, check if the associated $_POST variable is empty and return true if so and at the end false if none are empty:

    public static function areEmpty(...$fields) {
    
        foreach($fields as $field) {
            if(empty($_POST[$field])) {
                return true; // something was empty
            }
        }
        return false; // none were empty
    }
    

    You'll have issues with areSet() as well, because you loop $fields and set an array element of $setFieldsArray and then return if it is empty, which it will never be empty because you create array elements in it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算