Check empty fields with php

Asked

Viewed 3,637 times

1

My question is simple, but I’m not finding an answer. I have a form with 5 fields, but all fields are required instead of creating Empty() for each field, as I would to check that all fields are empty at once?

  • Want to do in Javascript or PHP itself ?

  • Hi Zoom. In php itself.

  • 7

    http://answall.com/questions/100493/validando-inputs-php/100500#100500

1 answer

1

You can check field by field with the function
empty() and is_null() or compare value with empty.
But all at once does not give.

I found this piece of code that might make it easier for you, I didn’t test 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.';
    }
}
  • Thank you all. The answer that most suited my needs was that of Edilson.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.