Validation array POST php

0

Below a table in my view, I need to check the column in the checkbox to not send the data to my other page, or check the POST time.

View:

<divclass="large-12 columns" id="tabela" style="overflow-y:  scroll; height: 80%; border: 0px solid;">
        <div class="TableCSS">
            <form method="POST" action="../controller/precontEnviaEmail">
                <table>
                    <thead>
                        <tr>
                            <th>Nome</th>
                            <th>Email</th>
                            <th>Nº Fatura em atraso</th>
                            <th>Ação</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" value="teste1" name="nome[]"></td>
                            <td><input type="text" value="[email protected]" name="email[]"></td>
                            <td><input type="text" value="666666666" name="fatura[]"></td>
                            <td><input type="checkbox" value="" name="valida[]"></td>
                        </tr>
                        <tr>
                            <td><input type="text" value="teste2" name="nome[]"></td>
                            <td><input type="text" value="[email protected]" name="email[]"></td>
                            <td><input type="text" value="666666666" name="fatura[]"></td>
                            <td><input type="checkbox" value="" name="valida[]"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <input type="submit" class="tiny button">
        </form>
    </div>

Precontroller:

$nome = $_POST['nome'];
$email = $_POST['email'];
$fatura = $_POST['fatura'];
$valida= $_POST['valida'];

//Validação
    
asked by anonymous 29.05.2018 / 21:09

2 answers

2

One way to do this is to generate an associative array from the form already relating the data of each record. Something like:

<tr>
  <td><input type="text" name="registros[0][nome]" value="teste1"></td>
  <td><input type="text" name="registros[0][email]" value="[email protected]"></td>
  <td><input type="text" name="registros[0][fatura]" value="666666666"></td>
  <td><input type="checkbox" name="registros[0][valida]" value="1"></td>
</tr>
  

Note: The index value, exemplified as 0, must be incremented manually.

So instead of generating four distinct arrays , only one will be created. For example, $_POST would look something like:

$_POST = [
    'registros' => [
        [
            'nome' => '...',
            'email' => '...',
            'fatura' => '...',
            'valida' => '...'
        ], [
            'nome' => '...',
            'email' => '...',
            'fatura' => '...',
            'valida' => '...'
        ], [
            'nome' => '...',
            'email' => '...',
            'fatura' => '...',
            'valida' => '...'
        ], 
        ...
    ]
];

In this way you can use the array_filter function to get only records that have valida equal to 1:

$validados = array_filter($_POST['registros'], function ($it) {
    return isset($it['valida']) and $it['valida'] == 1;
});
    
30.05.2018 / 13:49
0

In PHP , if checkbox is checked it will appear with its respective key, so just give unset() in array $_POST that has the key with checkbox checked, like this:

if(isset($_POST['valida'])){
    foreach ($_POST['valida'] as $key => $value){
        foreach ($_POST as $keyPost => $valuePost){
            unset($_POST[$keyPost][$key]);
        }
    }
}

In the first% of% it takes every% of% that has been marked;

In the second, for each checked checkbox, it checks the array foreach() and checkbox on all subarrays corresponding to $_POST key

    
29.05.2018 / 21:23