It is worth mentioning that using <input name="btn" type="submit" value="Enviar" />, the value of btn will always be present in the vector $_POST, because PHP treats the button itself as a form input (since it is a input). Thus, to consider only the "real" fields of the form, it is necessary to discard such vector value:
unset($_POST["btn"]);
And then check the remaining values. It is also worth noting that a string empty is considered by PHP as a possible value and therefore the function empty will always return false, since the vector will have elements with value string empty. To get around this, simply filter the vector according to its values:
array_filter($_POST);
Not passing the second parameter of the function array_filter, all values found to be false shall be removed and in this case, string empty is, by default, considered false in PHP. This way, would remain in $_POST only duly completed fields.
The code would look something like this:
unset($_POST["sub_btn"]);
if(empty(array_filter($_POST))) {
echo "empty";
}else {
echo "no empty";
}
I do not know if I understand very well. If a field is filled the
formwould be "valid" in your doubt?– Randrade
Like this : I have
nfields, if the guy clicks on Direct Ubmit with all empty, then writesempty, if you have a data field writeno empty...– MagicHat
I don’t think this is a good approach. However, I think it’s best to let someone who understands PHP answer. There’s something I don’t know about :p
– Randrade
If you have another approach to check if the form is being sent empty, without checking field by field, you are welcome...
– MagicHat
I think it would be best to do so http://stackoverflow.com/a/3190482/3228982. but it is still checked field by field
– Augusto
@Magichat I don’t know if this is possible. I’ve always seen people creating a function to check if the field is null and passing a
arraywith the field names. This is how the desired fields are checked. However, I don’t know anything that does this automatically in PHP.– Randrade
@Augusto this link code will check if there is a blank field and not if all are blank.
– Pagotti
@Pagotti, yes but you can use the same idea just by changing in
ifofemptyfor!emptyand give abreak. I think it would solve– Augusto