How to get checkbox value (on table) into the controller?

You still have DOM structure problems.

<form >
<table>
</form>
</table>

is illegal.

ALL tags must be nested. They can never interleave as they do in your code.

<form >
   <table>
   </table>
</form>

Also in your code there are more </div> closing tags than there are <div> opening tags.

I’ve cleaned up these basic errors and simplified the code without changing its intent and it works fine.

I’ve kept your variable names the same so, in theory, you should be able to run this code and get a working form for verification.

You’ll see I used an alternate syntax for foreach and if using ‘:’ instead of '{`. https://www.php.net/manual/en/control-structures.alternative-syntax.php

<div class="col"><div class="col">
    <h3>IMPRESSÃO DE BOLETOS</h3>
    <?= $this->Form->create(null, ['url' => ['action' => 'gerarPdfSelecionados']]); ?>
    <?= $this->Form->submit(
        'BAIXAR BOLETOS SELECIONADOS',
        ['action' => 'gerarPdfSelecionados'],
        ['confirm' => 'Deseja Imprimir Todos os Boletos Selecionados?', 'class' => 'btn btn-outline-primary statement']
    ); ?>
</div>

    <table class="table table-hover extra-slim-row">

        <thead>
        <th>SELECIONAR</th>
        <th> Nosso Numero </th>
        <th><?= '$this->Paginator->sort(\'data_vencimento\', \'Data Vencimento\');' ?></th>
        <th><?= '$this->Paginator->sort(\'valor\', \'Valor\');' ?></th>
        <th>Data Emissão</th>
        <th>Ação</th>
        </thead>

        <?php foreach ($payamentSlips as $payamentSlip) : ?>
            <tr <?= $payamentSlip->boleto_emitido ? ' style="background-color: lightgray"' : '' ?> >
                <td style="text-align: center">
                    <?php
                    echo $this->Form->control('flag.' . $payamentSlip->nosso_numero, [
                        'type' => 'checkbox',
                        'label' => false,
                        'data-payable-id' => $payamentSlip->id,
                    ]);
                    ?>
                </td>
                <td><?= '$payamentSlip->nosso_numero' ?></td>
                <td><?= '$payamentSlip->data_vencimento;' ?></td>
                <td <?= '$this->element(\'numero_formatado\');' ?></td>
                <td><?= '$payamentSlip->data_emissao' ?></td>
                <td>
                    <?= $this->Form->button(
                        'DOWNLOAD DO BOLETO',
                        array('type' => 'submit', 'formaction' => ['gerarPdf/', $payamentSlip->id])
                    ); ?>
                </td>
            </tr>
        <?php endforeach; ?>

        <?php if (count($payamentSlips) == 0) : ?>
            <tr>
                <td colspan="6" class="no-data-found">Nenhum registro encontrado</td>
            </tr>
        <?php
        endif; ?>

    </table>
<?php echo $this->Form->end(); ?>

1 Like

When you said DOM problems I tried to put form after table and still got the same issue.
also I don’t get it when you changed some code, are you using different cake php version than me? I’m using cake php 3.8 . You said your code is working, well I tested here and still same problem, it show array of 7 values which all are 0 when checked. Also you put strings before the $this->element’ and it output the string…
Also the changes you did made my single download button doesnt work, it add a % before the value, example: id 2091, it comes with %2091 ( I deleted the space between $payamentSlip->id and still same issue, I think i need clear cache here and test more)
also the $this->sorter didnt worked too, even removing the ’ strings

I suppressed a lot of output by turning statements into literals since I didn’t have actual data to support them and they do not impact the problem.

I’m using the same cake version.

Since you continue to have this problem you need to simplify the page (for testing) to focus on the issue. This is a strategy that will help in many situations. And I used a mild form of simplification to get a test page running on my end.

I eliminated pagination and the output of the cell content that didn’t matter to the form. This way I didn’t have to write unnecessary to test and I eliminated things that might unexpectedly interact.

If you can get a stripped down form working, then you can build your page back up, checking function as you go. If the form stops working then you have a clue about what is ACTUALLY causing the problem.

Here are the screen captures of my setup:

Here is the controller code. The first method sets up my data and renders the page, the second captures and outputs the post.

<?php


namespace App\Controller;


use Cake\ORM\Entity;

class ForumTestController extends AppController
{
    public function forum()
    {
        $data = [
            [
                'nosso_numero' => '01',
                'boleto_emitido' => 1,
                'id' => 3
            ],
            [
                'nosso_numero' => '07',
                'boleto_emitido' => 1,
                'id' => 4
            ],
            [
                'nosso_numero' => '044',
                'boleto_emitido' => 0,
                'id' => 5
            ],
            [
                'nosso_numero' => '0345',
                'boleto_emitido' => 0,
                'id' => 6
            ],
        ];
        $payamentSlips = [];
        foreach ($data as $datum) {
            $payamentSlips[] = new Entity($datum);
        }
        $this->set('payamentSlips', $payamentSlips);
    }

    public function gerarPdfSelecionados()
    {
        debug($this->request->getData());die;
    }
}
1 Like

Thanks! I feel so dumb now it was this script

  $(document).ready(function() {

      $("input:checkbox").iCheck({
          checkboxClass: 'icheckbox_flat-blue',
          radioClass: 'iradio_flat',
          increaseArea: '20%'
      });
  });
</script>

Why it makes that problem happen? I’m only putting a class inside it

also another doubt that I have, after doing $data = $this->request->getData(‘flag’);
and foreach($data as $datum) {
how I can get the "id"of the checkbox?
if i do $datum it will get the value (1 or 0 )
}

I don’t know what your javascript is doing. It looks like you’re using the jQuery iCheck tools and I’m not familiar with those.

I’m not sure what your asking about the foreach. Given this sample data from my test setup:

//request->getdata('flag') = 
 [
	'01' => '1',
	'07' => '1',
	'044' => '0',
	'0345' => '0'
]

Then

foreach ($this-request->getdata('flag') as $key => $datum) {
   echo $key . ' is set to ' . $datum;
}

will output

01 is set to 1
07 is set to 1
044 is set to 0
0345 is set to 0

If you want the id rather then the current nosso_numero just change your Form->control() arguments to

echo $this->Form->control('flag.' . $payamentSlip->id, [
                        'type' => 'checkbox',
                        'label' => false,
                        'data-payable-id' => $payamentSlip->id,
                    ]);

Which will change the name attribute and hence, the post array structure.

1 Like

Since this is a new topic I’d suggest starting a new thread. There may be people with an answer but they aren’t going to be digging into this old topic.

1 Like