Check that all $_POST was shipped without using too many ifs

Asked

Viewed 1,540 times

3

Good morning,

I have to check that all the form fields, sent as $_POST, were not blank, this until I solved. But I wanted to see a way to not need to use too many if/Elif to check every $_POST index 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 tried to only use if(Empty($_POST)) but it didn’t work, I let it pass even with the blank fields.

5 answers

6

You can simplify the logic considerably 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 whether each is defined in $_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
}

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

4

You can do it with a foreach, 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 within the variable $_POST some empty field is found, displays a message and finishes the program.

3

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

3

From the php5.5 empty() started to evaluate expressions and not only variables. One way to solve the problem is to connect all fields through conjunctions (commonly E logical) and let the if handle the result of the expression.

Important: if any of the values contain zero it will be evaluated as false which will generate a false positive, that attentive to this detail.

$_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

2

I work as follows in my projects:

1 - I leave the input obligatorily, placing the required:

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

Thus, the form is not sent without this 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 field name, assigns the value to the variable, if it does not receive, assigns NULL.

You can also use the isset PHP. Which checks whether the variable was started:

$nome = isset($_POST['nome']) ? $_POST['nome'] : NULL;

Browser other questions tagged

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