My question is simple, but I'm not finding an answer. I have a form with 5 fields, however all fields are required instead of I create empty () for each field, how would I check to see if all the fields are empty at one time?
My question is simple, but I'm not finding an answer. I have a form with 5 fields, however all fields are required instead of I create empty () for each field, how would I check to see if all the fields are empty at one time?
You can check field by field with the function empty()
and is_null()
or compare the value with empty.
But all at once can not.
I found this piece of code that can make it easier for you, I have not tested it.
// Cria as variáveis dinamicamente
foreach ( $_POST as $chave => $valor ) {
// Remove todas as tags HTML
// Remove os espaços em branco do valor
$chave = trim( strip_tags( $valor ) );
// Verifica se tem algum valor em branco
if ( empty ( $valor ) ) {
$erro = 'Existem campos em branco.';
}
}