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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?