0

Using Cakephp 2 i am trying to select one or many records clicking into a checkbox, from a list named my_id[] :

<input type="checkbox" name="my_id[]" value="<?php echo $Myobj['My']['id']; ?>

I have a link to the controller processBatch method but did not know how to pass the array of selected data using the checkbox into the view file.

<?= $this->Html->link('Batch process','proccessBatch')?>

I am trying :

public function proccessBatch( ) //array of $ids  
{        
    pr($this->request);

But did not see $this-request->data. How can i get the checkbox selected values ?

1
  • You can post data into the proccessBatch() function by submitting form. then you will get the data into "$this->request->data" array. Commented Oct 15, 2016 at 16:10

1 Answer 1

1

I have created a sample for you below.

Here is my view

    <?php
    //sample list of items with 'id'=>'name
    $arrayList = [
        0=>'item 1',
        1=>'Item 2',
        2=>'Item 3'
    ];
    //create the form
    echo $this->Form->create('listofitems',array('novalidate' => true));
    //generate the checkboxes by looping through the items(this is just one way of doing it)
        for($i=0;$i<count($arrayList);$i++){
            //concatenate the value of the id ($i in my case) with the name
            // of the field to uniquely identify it.
            echo $this->Form->checkbox("my_id".$i);
        }
    echo $this->Form->end('save');//end form and save button
    ?>

Here is my controller and its action

<?php
App::uses('AppController', 'Controller');
class SampleController extends AppController {

    public function arrays(){
        pr($this->request->data); //just pr the posted data
    }

}
?>

And the result of the pr line in the controller

Array
(
    [listofitems] => Array
        (
            [my_id0] => 1
            [my_id1] => 0
            [my_id2] => 1
        )

)

Hope this helps you with your question. Do note that only the checked items have a value of 1 while unchecked ones have a value of 0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.