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条)

报告相同问题?