Verify that all $ _POST was sent without using many ifs

3

Good morning,

I have to check if all fields of the form, sent as $ _POST, have not been blank, this is until I solved it. But I wanted to see a way to not need to use many if / elif to check every index of $ _POST would:

if(empty($_POST['nome'])){?>
    <p>Campo NOME em branco</p>
<?php}
elif(empty($_POST['cpf'])){?>
    <p>Campo CPF em branco</p>
<?php}
elif(empty($_POST['endereco'])){?>
    <p>Campo ENDERECO em branco</p>
<?php}
...
else{

$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$endereco = $_POST['endereco'];
?>

I just tried to use if (empty ($ _ POST)) but it did not work, I let it go even with the blank fields.

    
asked by anonymous 22.06.2018 / 16:06

5 answers

6

You can greatly simplify logic if you use an auxiliary array for the fields you are interested in. In this case you can iterate the fields with a foreach and test if each is set to $_POST , and display its blank field text when it is not.

Example:

$valido = true;
$campos = Array("nome", "cpf", "endereco");

foreach ($campos as $campo){
    if (empty($_POST[$campo])){
        echo "<p>Campo $campo em branco</p>";
        $valido = false;
    }
}

if ($valido){
    $nome = $_POST['nome'];
    $cpf = $_POST['cpf'];
    $endereco = $_POST['endereco']; 
    //resto do código
}

In this way even if you have more fields to check if they are filled, you just need to add them in the $campos array.

    
22.06.2018 / 16:19
3

Try to check if the value is empty, even if it is not filled it will return null and null is already considered a value, it also verifies if it exists, by placing the! isset. It would look like this: if ( !isset( $_POST ) || empty( $_POST ) )

    
22.06.2018 / 16:19
3

You can do this with a foreach , it would look something like this:

foreach ($_POST as $key => $value){
    if (empty($value)){
        echo "<p>Campo ".$key." em branco</p>";
        exit;
    }
}

$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$endereco = $_POST['endereco'];

If a blank field is found within the $_POST variable, it displays a message and ends the program.

    
22.06.2018 / 16:22
3

From php5.5 empty() started evaluating expressions, not just variables. One way to solve the problem is to connect all fields through conjunctions (common E logical) and let if take care of the expression result.

Important: If any of the values contains zero it will be evaluated as false which will generate a false positive,

$_POST = array('id' => '', 'nome' => 'fulano', 'email' => '[email protected]');

if(!empty($_POST['id'] && $_POST['nome'] && $_POST['email'])){
    echo 'campos válidos';
}else{
    echo 'campos inválidos';
}

echo "<pre>";
print_r($_POST);

Example - ideone

    
22.06.2018 / 16:27
2

I work as follows in my projects:

1 - I leave input obligatorily, putting required :

<input type="text" name="nome" placeholder="Nome" required>

Thus, the form is not sent without this filled field.

2 - In my PHP file that receives the value, I do it as follows:

$nome = $_POST['nome'] ? $_POST['nome'] : NULL;

I use a ternary conditional. If my PHP file is receiving the name field, assigns the value to the variable, if it does not, assign NULL . Home You can also use isset of PHP. That checks whether the variable was started:

$nome = isset($_POST['nome']) ? $_POST['nome'] : NULL;
    
22.06.2018 / 16:20