user3034219 user3034219 - 8 months ago 31
PHP Question

Cakephp 1.2 passing data from variable number of checkboxes to form in controller

Im working on a piece of code to alter permissions for users of an app. Currently, each permission is added one at a time, Im revising it to be added in a faster way, being able to select multiple permissions to add at once.

The checkboxes aren't going to be the same number, since they are only the permissions the user doesn't yet have. It needs to pass an id with the permissions.

The problem is, my checkbox form doesn't appear to be sending any data to the controller. Upon submitting, it just loads a blank page with the url of the controller.

Heres the view code, that generates the checkbox form


if (!empty($lstAvailablePermissions)) {
echo $form->create('Administrator', array('action'=>'addPermission'));
echo $form->input('id');

foreach($lstAvailablePermissions as $key){

echo "<br>";
echo $form->input(

'value' => $key,
'name' =>'data[Administrator][permission]['.$c.']'

echo $form->button(__('Add', true), array('type'=>'submit', 'class' => 'button', 'style'=>'padding: 2px; font-size: 12px;'));
echo $form->end();

and the method in the controller

function AddPermission() {
if (empty($this->data)) { $this->RedirectWithFlash(__("Only POST Requests", true), "/administrators"); }

ErrorLogWarning("This is the form data sent to the controller", $this->data);

for ($i = 0; $i < $length; $i++){
array('model' => 'Administrator', 'foreign_key' => $this->data['Administrator']['id']),

$this->RedirectWithSuccessFlash(__("Permission added", true), array('action'=>'edit', $this->data['Administrator']['id']));

can anyone help me figure out whats going on? I assume ill need to loop through the data once its in the controller, but it doesn't even send the error message as of right now.

EDIT: problem appears to be fixed by adding the name property at the bottom of the form echo, which send it as an array which can be looped through in the controller.


I think the problem might be that you specify 'action'=>'addPermission' when you create the form, but your controller action is named AddPermission (capital A). Try changing to 'action'=>'AddPermission'.

Also, I think you might only get the last permission input because you're adding the permission inputs in a loop. Try indexing the inputs like this:

foreach($lstAvailablePermissions as $i => $key) {

    echo "<br>";
    echo $form->input(