Multiple values in checkbox
Hi,
Just wondering if you can help.
I have a checkbox name 'fruit' that has multiple values e.g. apple, orange, grape
I'm looking to select multiple values and send this to a SQL table. Let's say I select all three fruits.
My problem is every time I send to the SQL table, it always display the number 1 in the field instead of "apple, orange, grape"
This is the PHP code I used:
echo $this->Form->Checkbox('Fruit', 'apple');
echo $this->Form->Checkbox('Fruit', 'orange');
echo $this->Form->Checkbox('Fruit', 'grape');
Any help would be appreciated.
Thank you
Best Answer
-
R_J Admin
The correct form element would be a CheckboxList. You use it like that:
Controller
// Get your fruits form somewhere. $fruits = ['apple', 'orange', 'grape']; // CheckBoxList expects value => label array items. $fruits = array_combine($fruits, $fruits); $sender->setData('Fruits', $fruits);
View
echo $this->Form->checkBoxList('Fruits', $this->data('Fruits'));
If I remember it correct, after post back
$sender->Form->getValue('Fruits')
would return an array of checked values (not an array of value => item, but only value, but please check that for yourself...)6
Answers
The correct form element would be a CheckboxList. You use it like that:
Controller
View
If I remember it correct, after post back
$sender->Form->getValue('Fruits')
would return an array of checked values (not an array of value => item, but only value, but please check that for yourself...)