• support@answerspoint.com

CakePHP multiple select “selected” is not working

5987

I have a multiple select input in my edit form:-

<?php echo $this->Form->input('Article.tags', array('type' => 'select', 'multiple' => true, 'options' => $tags, 'selected' => array($selected))); ?>

When echo-ed, the $selected variable will look like this:-

"MySQL", "PHP"

However, the input does not automatically select the option that matches the tag.

However, when I manually put in the selected option, it will automatically select that two option; e.g.

<?php echo $this->Form->input('Article.tags', array('type' => 'select', 'multiple' => true, 'options' => $tags, 'selected' => array("MySQL", "PHP"))); ?>

Is there anyway to fix this? Thanks.

1Answer


0

I was looking at the wrong direction. I assumed that the selected values should be in string form with quotes and comma separated. (e.g. "MySQL", "PHP", "jQuery").

Instead, it should be in array format without any quote and comma. e.g.

Array
(
    [0] => MySQL
    [1] => PHP
    [2] => jQuery
)

Once I've got the array sorted out I pass it into view; e.g. $this->set('selected', $myArray);

Then on the form, I would just have to echo it out like this:-

<?php echo $this->Form->input('Article.tags', array('type' => 'select', 'multiple' => true, 'options' => $tags, 'selected' => $selected)); ?>
  • answered 8 years ago
  • Sunny Solu

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 5987 times
  • active 8 years ago

Best Rated Questions