Reputation: 2149
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 ?
Upvotes: 0
Views: 1595
Reputation: 115
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
Upvotes: 1