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?
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:
Here is PHP function to check it and how to use.
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
ifcondition.