无法从 Codeigniter 中的 Multiselect 下拉列表中获取 post 值

Unable to post values from Multiselect drop down in codeigniter

我无法从多个 select 下拉列表中 post 值。实际问题出在本节中用户的编辑配置文件部分,我无法从数据库中 post 已经 selected 的值。 这是我的html部分

<select name="drop_caste[]" id="cast_to" class="form-control new-two"   size="8" multiple="multiple">
<?php foreach($selectedcast->result() as $row): ?>
<option value="<?php echo $row->cid; ?>" ><?php echo $row->caste ; ?></option>
<?php endforeach; ?> </select>
</select>

这是我用来 post 值

的代码
$this->input->post('drop_caste');

您需要将 drop_caste 添加到数组中作为

$data['drop_caste'] = $this->input->post('drop_caste');
print_r($data);// here you get your value

你可以使用foreach loop

foreach($this->input->post("drop_caste") as $drop_caste){
    echo $drop_caste;
}

你可以直接把POST数组改成字符串

$caste = implode(',',$this->input->post("caste"));

JS:

在某些操作上触发:

var config = { learn : [] };

$('#learn_multiselect option:selected').map(function(a, item){
   config.learn.push( { "id" : item.value } );
});

   //write code for ajax to send this config object using ajax to your controller
   //something like this

    $.ajax({
        url: $url,
        method: $method,
        data: $dataArr,
        dataType: $dataType,
        contentType: $contentType
    }).done(function(data){
        //handle as required
    }).fail(function(data){
        //handle as required
    });

HTML:

<select id="learn_multiselect" multiple="multiple">
                      <option value="1">Science</option>
                      <option value="2">Maths</option>
</select>

现在您应该能够在控制器中获取 POSTED(或您在 AJAX 中使用的任何方法)数据。