0

I trying to update a list of students class by checking on the checkboxes with a selection from the dropdown menu. I've selected four checkboxes. Hence, the debug correctly show 248,268,244,1220. but i got a error Warning (2): Illegal string offset 'id' [APP/controllers/customers_controller.php, line 728] due to "classroom_id". Is there anyways just displaying the list of selected checkboxes id only?

I am trying to update multiple records/rows by selecting the checkboxes. How can I send multiple selected ID to the controller?

Any helps will be greatly appreciated.

<?php echo $form->input('classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield')); ?>

<?php foreach ($students as $student) { ?>
<?php echo $form->input('Customer.'.$student['Customer']['id'].'.id', array('type' => 'checkbox', 'id' => "admin_checkbox_".$student['Customer']['id'], 'label' => false)); ?> 
<?php } ?>

CONTROLLER

foreach($this->data['Customer'] as $key => $item) {
    if ($item['id']) {
            Debugger::Dump($this->data['Customer'][$key]);
    }
}

OUTPUT

Warning (2): Illegal string offset 'id' [APP/controllers/customers_controller.php, line 728]

"classroom_id"

248

268

244

1220

3 Answers 3

1

The reason why you're getting 5 values instead of 4 is because of the classroom_id input:

<?php echo $this->Form->input('classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield')); ?>

Since you don't include the model's name in the input's name (e.g. Customer.classroom_id), it makes an assumption that you want to include it in the current model (assuming you didn't manually specify a model in the form element, of course).

This is correctly returning 5 values.

In your controller, however, you're assuming that the only data you're going to get is the checkboxes and not the select box. You're looping through ALL the "Customer" form data, not just the checkboxes.

There are 2 ways we could fix this:

Option #1: (Recommended) Modify your form input, and then update your loop

<?php
echo $this->Form->input('Customer.classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield'));

foreach ($students as $student) {
    echo $this->Form->input('Customer.id.'.$student['Customer']['id'], array('type' => 'checkbox', 'id' => "admin_checkbox_".$student['Customer']['id'], 'label' => false));
}
?>

Note: I switched the input's first parameter from 'Customer.'.$student['Customer']['id'].'.id' to 'Customer.id.'.$student['Customer']['id'] This allows you to access your POSTed data in an array. The resulting form input will look like (where <student id> is your actual student id):

<div class="input checkbox">
    <input type="hidden" name="data[Customer][id][<student id>]" id="admin_checkbox_<student id>_" value="0">
    <input type="checkbox" name="data[Customer][id][<student id>]" id="admin_checkbox_<student id>" value="1">
</div>

You can then loop through each checkbox separate in your controller, which should look something like:

foreach ($this->data['Customer']['id'] as $id => $checked) {
    if ($checked) {
        Debugger::Dump($this->data['Customer'][$id]);
    }
}

Option #1 is recommended, because it ensures your data is properly separated. You can then loop through just the data you need which is faster.

Option #2: The other option, which I don't recommend, is to use isset() to loop through all data but only act on the data you want

foreach ($this->data['Customer'] as $key => $item) {
    if (isset($item['id']) && $item['id']) {
        Debugger::Dump($this->data['Customer'][$key]);
    }
}
0
0

If you sure the warning is not important use error_reporting(E_ERROR);

1
  • but it should only return 4 values. currently it returned 4 checked values with the classroom_id in the for each loop.
    – DANLEE
    Apr 23, 2013 at 5:25
0
Instead of creating single single check boxes you should use proper format of cakephp. Please check below:

echo $this->Form->input('Model.field', array(
    'type' => 'select',
    'multiple' => 'checkbox',
    'options' => $students
    )
));

In above code $students is holding your check boxes values like [0]=male, [1]=female.

In your controller, you will one value same like you get value of textbox.
1
  • Hi Adiya. I am not looking for multiple checkbox. I am trying to update multiple records/rows by selecting the checkboxes. Thanks. How can i send multiple selected ID to the controller?
    – DANLEE
    Apr 23, 2013 at 22:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.