Search code examples
phpissetis-empty

Confused by how !empty() works in my if-condition


I'm confused by what counts as empty in this code logic. I have a variable amount of table rows (additional rows are dynamically inserted via ajax) that each accept four inputs. The logic I want is for a particular row - whose inputs are incomplete - to be unable to update a database:

if ((isset($_POST['_branch']) && !empty($_POST['_branch'])) 
&& (isset($_POST['_day']) && !empty($_POST['_day']))
&& (isset($_POST['_starttimepicker']) && !empty($_POST['_starttimepicker']))
&& (isset($_POST['_endtimepicker']) && !empty($_POST['_endtimepicker']))) {
    $b_id = $_POST['_branch'];
    $w_date = $_POST['_day'];
    $s_time = $_POST['_starttimepicker'];
    $e_time = $_POST['_endtimepicker'];

// ... rest of code comes here
}

The code snippet where the four inputs are handled:

<td>
    <input class="form-control" type="text" id="_starttimepicker" name="_starttimepicker[]"  placeholder="Enter Time" />
</td>
<td>
    <input class="form-control" type="text" id="_endtimepicker" name="_endtimepicker[]"  placeholder="Enter Time" />
</td>

When I have start time and end time "empty" (haven't interacted with them; the placeholder remains as "Enter Time"), the if-condition logic above still processes and the database is ultimately updated (with bad values). Even when I try to play around with the default value parameter (I have value = "" and "red"):

<td>
    <input class="form-control" type="text" id="_starttimepicker" name="_starttimepicker[]" value="" placeholder="Enter Time" />
</td>
<td>
    <input class="form-control" type="text" id="_endtimepicker" name="_endtimepicker[]" value="red" placeholder="Enter Time" />
</td>

The if logic still processes (also, the relevant database value where I had value="red" remains as "". I thought it would have taken the default value "red", so maybe I'm misunderstanding this).

What am I misunderstanding? Any advice?


Solution

  • I see that you use input arrays. To check empty form fields that are input arrays, you need more complicate than just empty().

    Example HTML form:

    <form method="post">
        <input class="form-control" type="text" id="_starttimepicker" name="_starttimepicker[]"  placeholder="Enter Time" />
        <input class="form-control" type="text" id="_endtimepicker" name="_endtimepicker[]"  placeholder="Enter Time" />
        <button type="submit">Submit</button>
    </form>
    

    Here is PHP function to check it and how to use.

    $_starttimepicker = ($_POST['_starttimepicker'] ?? []);
    // in PHP older than 7.0 use (isset($_POST['_starttimepicker']) ? $_POST['_starttimepicker'] : []) instead.
    $_endtimepicker = ($_POST['_endtimepicker'] ?? []);
    
    /**
     * Check if ANY fields are empty.
     *
     * @param array $fields The input form field, one by one.
     * @return bool Return `true` if one of a form field is empty. Return `false` for otherwise.
     */
    function isEmptyFields(...$fields) 
    {
        $foundEmpty = false;
        foreach ($fields as $field) {
            if (is_array($field) && !empty($field)) {
                foreach ($field as $index => $value) {
                    if (empty($value)) {
                        $foundEmpty = true;
                        break;
                    }
                }
            } else {
                if (empty($field)) {
                    $foundEmpty = true;
                    break;
                }
            }
        }
        return $foundEmpty;
    }
    if (!isEmptyFields($_starttimepicker, $_endtimepicker)) {
        // if all fields are NOT empty.
        echo 'save your data.';
    }
    

    Tested submit the form by filled all fields, the result is in condition (save your data.) but if empty just one, it will not goes into if condition.