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.

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

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程