dongyong9224 2017-03-13 07:17
浏览 50
已采纳

在codeigniter中使用select multiple的内容填充PHP数组

In my view I have a form select multiple box where I add items using a select dropdown and a button like this.

This is how the code of the select multiple box looks after adding 3 items to it.

<label for="nombresFamilia" class="col-md-4 control-label"></label>
<div class="col-md-6">
   <select multiple id="nombresFamilia" name="nombresFamilia[]" class="form-control">
       <option value="CABLES">CABLES</option>
       <option value="PCR">PCR</option>
       <option value="POSTES">POSTES</option>
   </select>
</div>

After submitting the form information, I want to save the items inside the select multiple into an array. This is the code I have in my controller.

function add()
{   
    .
    .
    .
    $this->form_validation->set_rules('nombresFamilia[]','NombresFamilia');

    if($this->form_validation->run())     
    {   
        .
        .
        .

        $params_familia = $this->input->post('nombresFamilia[]');  
    }
}

However, when I try to var_dump the array

<?php
    echo "<pre>";
    var_dump($params_familia);
    echo "</pre>";
?>

I get the message: "Undefined variable: params_familia".

What am I doing wrong?

  • 写回答

3条回答 默认 最新

  • duanqizao6492 2017-03-14 00:16
    关注

    you could pre-select the options of the select, do something like this
    VIEW

    <select name="nombresFamilia[]" class="" multiple="multiple">
      <option value="CABLES" selected="selected">CABLES</option>
      <option value="PCR" selected="selected">PCR</option>
      <option value="POSTES" selected="selected">POSTES</option>
    </select>
    


    CONTROLLER

    $nombresFamilia = $this->input->post('nombresFamilia');
    // remove [] in nombresFamilia
    
    echo "<pre>";
    var_dump($params_familia);
    echo "</pre>";
    


    PRODUCES

    Array
    (
        [0] => CABLES
        [1] => PCR
        [2] => POSTES
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python使用pulp线性优化时报错
  • ¥15 为什么我的uibot导入py模块出错呀。py文件放在了uibot里对应的python文件夹了,卸了重安也不行
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端