ruby_noobie
ruby_noobie

Reputation: 205

How can I create an array of numbers based on which checkboxes are checked in a form?

I need to allow multiple values to be selected, and I want to create an array of the selected values. Here is what I have so far:

<p>Select Multiple Values
    <?php echo $this->Form->create(); ?>
    <?php foreach($possibilities as $possibility):
     echo $this->Form->input($possibility['name'], 
         ['type' => 'checkbox', 'value' => $possibility['id']]);
    endforeach; ?>
    <?php echo $this->Form->button(__('Submit'));
    echo $this->Form->end(); ?></p>

How can I check this form to see what values have been selected and store them in an array?

Upvotes: 1

Views: 36

Answers (1)

Eymen Elkum
Eymen Elkum

Reputation: 3141

You can try this script:

echo $this->Form->select('possibilities', $possibilities, [
    'multiple' => 'checkbox'
]);

In controller simply:

$selected = $this->request->data('possibilities');

You can visit this CookBook Page

Upvotes: 1

Related Questions