Validation POST php array

Asked

Viewed 213 times

0

Below a table in my view, I need that when marking the column in the checkbox is not sent the data to my other page, or do a check when searching the POST.

inserir a descrição da imagem aqui

View:

<div class="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:

Here validate if checkbox is filled do not take the table row data only take what is not marked.

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

//Validação
  • Have you tried anything in the validation you need to do?

  • I tried but I didn’t post because it won’t add what I need, I just need it to validate. I know how to do if it was only 1 given, however it is array. It would be something simple like if isset checkbox takes the data from the table row for me to deal with later if you have marked discard, something like that.

  • @Kevin. F , I edited my answer, agr works

2 answers

2


One way to do it is to generate a array associative 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 value of the index, exemplified as 0, must be incremented manually.

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

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

This way you can use the function array_filter to pick up only the records they have valida equal to 1:

$validados = array_filter($_POST['registros'], function ($it) {
    return isset($it['valida']) and $it['valida'] == 1;
});
  • Cool, so I think it gets more organized. A doubt I do a count to dynamically add the number in the records[0] ?

  • @Kevin. F Depends on how you generate HTML. If it is via PHP or JS, yes, you can use a variable count. The important thing is to keep the same value between the columns of the same record, otherwise each row will generate a different record, doing something like registros[]['nome'].

  • This array_filter function stores the data within the $validated variable if the validation is 1 is this ?

  • @Kevin. F Exact.

  • I am checking a checkbox for example and giving a print_r($validated); after the ta function returning the array blank, there is something else to do ?

  • Depends on how you’re doing array original and its function. Can post to https://repl.it?

  • The function I commented on is yours up there in the reply, I saw that returned two errors Undefined index: registros and array_filter() expects parameter 1 to be array, null given in

Show 3 more comments

0

In the PHP, if the checkbox is marked it will appear with its respective key, so just give unset() in the array $_POST who has the key with the checkbox marked, thus:

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

In the first foreach() he takes every checkbox that was marked;

In the second, for each checkbox marked, it checks the array $_POST and of unset() in all subarrays corresponding to the key of the checkbox;

  • 2

    Remember that $_POST['valida'] will be a array with all screen checkbox and not just one.

  • KKKK face that vacillated, I will edit

  • With this your code ta picking up only the data of the teste2 indifferent of what I mark in the checkbox.

  • Strange, I did a test with your code and it worked, until I added 5 more tests, and it worked perfectly

  • You get the other posts inside the last foreach ?

  • yes, the last one will erase everyone who has the same key

Show 1 more comment

Browser other questions tagged

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