Jquery将值传递给控制器​​(codeigniter)

时间:2017-01-18 21:19:13

标签: jquery mysql ajax codeigniter-3

我有一个我无法解决的问题。我的目标是向所有成员展示 在列表中选择的组。到目前为止,我已经到了,我可以看到 控制台中选定的组,但我无法弄清楚如何将其传递给控制器​​。希望有人理解我的问题。我的代码如下。

Members_view.php

<select id="group_id">

<?php foreach($groups->result() as $row){?>

<option value="<?php echo $row->group; ?>" ><?php echo $row->group; ?></option>
  <?php } ?>
</select>

 <ul>
 <?php foreach($members->result() as $row){?>
 <li><?php echo $row->firstname." ".$row->lastname; ?></li>
<?php } ?>
 </ul>

的jquery.js

$('#group_id').change(function(e) {
    e.preventDefault();
    var base = '<?=base_url()?>';
    var grp = $(this).val();
    $.ajax({
        type: "POST",
        url: base + "members/members",
        data: { "group_id": grp },
        dataType: "html",
        cache: false,
        success: function() {
            console.log(grp);
        }
    });
});

Members.php

 function members(){
    $group_id = $this->input->post('group_id',true);

    $data['groups'] = $this->mdl_members->_all_groups();
    $data['members'] = $this->mdl_members->_members($group_id);
    $this->load->view('members_view',$data);
}

Mdl_members.php

function _all_groups(){
  $sql = $this->db->query("select * from groups");
  return $sql;
}

function _members($id){
$sql = $this->db->query("select * from members WHERE group_id = '$id'");
return $sql;
}

最好的问候。

1 个答案:

答案 0 :(得分:0)

Mdl_members.php :模型返回数据,我更喜欢arrays

function _all_groups(){
  $sql = $this->db->query("select * from groups");
  return $sql->result_array();
}

function _members($id){
$sql = $this->db->query("select * from members WHERE group_id = '$id'");
return $sql->result_array();
}

Members.php :看看新的members_view()方法。他将view _all_groups()数据调用select

function members_view(){
        $data['groups'] = $this->_all_groups();
        $this->load->view('members_view',$data);
    }

function members(){
    $group_id = $this->input->post('group_id',true);
    $members = $this->_members($group_id);
    echo json_encode($members);
}

Jquery.js :只需success: function(resp)中的某些行来json获取members()的响应,并将其放入<ul id="resp">

<script>
$(function () {
    $('#group_id').change(function(e) {
        e.preventDefault();
        var base = '<?=base_url()?>';
        var grp = $(this).val();
        $.ajax({
            type: "POST",
            url: base+"members/members",
            data: {"group_id": grp},
            dataType: "JSON",
            cache: false,
            success: function(resp) {
                var li = "";
                $.each(resp, function (i, v) {
                    li+="<li>"+v+"</li>";
                });
                console.log(li);
                $('#resp').html(li);
            }
        });
    });
});
</script>
创建

Members_view.php <ul id="resp">以显示回复数据。

<select id="group_id">
    <?php foreach($groups as $row){?>
        <option value="<?=$row['group'];?>">
            <?=$row['group'];?>
        </option>
    <?php } ?>
</select>
<ul id="resp">Will be shown here!</ul>