【问题标题】:Error while getting multiple values from select cakephp从选择 cakephp 获取多个值时出错
【发布时间】:2016-08-28 08:56:33
【问题描述】:

我正在尝试获取多个值以将它们存储在 cakephp 3.x 的数据库中,但我无法获取我选择的所有值。只有一个。

在我看来:

<select name="internalDestinations[ids]" id="internalDestinations-ids" multiple="true" class="...">
<?php
    foreach ($users2 as $i): ?>
       <option value="<?= $i['email'] ?>"> <?= $i['email'] ?> </option>
    <?php endforeach; 
    ?>
</select>   

在我的控制器中:

if($this->request->is('post')){
     $alarm->internalDestinations=$this->request->data['internalDestinations']['ids'];
     $this->log($this->request->data['internalDestinations']);
}

在我的输入中选择多个项目,我只得到一个:

Array
(
     [ids] => xxxxxxx@xxxxx.xxx
)

有什么帮助吗? 非常感谢

【问题讨论】:

    标签: cakephp-3.0 multiple-select multipleselection


    【解决方案1】:

    你可以这样做

    name="internalDestinations[ids][]" 
    

    但为什么不使用FormHelper

    //first of all let's create an array with email both in the keys and in the values
    
    $users3 = Cake\Utility\Hash::combine($users2, '{n}.email', '{n}.email');
    
    // then let's use cake dotted notation to create arrays
    
    <?= $this->Form->input('internalDestinations.ids', [
            'type' => 'select', 
            'multiple' => true, 
            'options' => $users3
        ]); ?>
    

    【讨论】:

    • 我不知道 internalDestinations 是什么,也不知道你想要实现什么。我向您展示了如何传递多个值。如果您尝试保存关联模型,请查看手册 herehere
    • 基本php:使用implode函数。但是让我说将逗号分隔的值存储到单个字段中是一个坏主意。
    猜你喜欢
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2021-12-26
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多