Tuesday, October 7, 2014

Re: get the id's from checkbox list

Hello.

I think you need to add some index to the checkboxes (so it would be rather an array), because if there are several items in $tutor variable, you keep creating checkboxes with the same name - data[Model][available]. So every checkbox has the same name and you keep "overwriting" them if the $tutor array has more than 1 element.
That causes the situation that only the data from the last checkbox is being sent within the form.

I would do it like this:
$i=0;
foreach($tutor as $item) {
echo $this->Form->checkbox('available.'.$i, array('value' => $id, 'checked' => 0, 'hiddenField'=>false);
$i++;
}

or even without the $i variable  (notice the dot after the "available"):

foreach($tutor as $item) {
echo $this->Form->checkbox('available.', array('value' => $id, 'checked' => 0, 'hiddenField'=>false);
}

hiddenField is set to false for the purpose of not displaying a hidden field for checkbox.

Then in the controller you can get the array of checked values of all checkboxes like this:

$ids=array()
foreach($this->request->data[Model]['available'] as $item) {
$ids[]=$item;
}
Just remember to add 'hiddenFIeld' => false to checkboxes.

If your request will be blackholed, simply add the following code at the beggining of your controller:
public function beforeFilter() {
parent::beforeFilter();
if(in_array($this->params['action'],array('name_of_the_controllers_action'))) {
$this->Security->unlockedFields=array('available');
}
}

or put the $this->Form->unlockField('available').

Hope this helps.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: