简体   繁体   English

使用codeigniter在视图上从javascript调用控制器

[英]call controller from javascript on view using codeigniter

hey guys i am facing trouble in calling controller method from javascript pls help . 大家好,我在从javascript pls help调用控制器方法时遇到麻烦。 .

my view is 我的看法是

<script type="text/javascript">
    function kccbranchselect()
    {
        $.ajax({
        type : 'POST',
        data : 'addreceiptkccbranchid='+ $('#addreceiptkccbranch').val(),
        url : '<?php echo base_url();?>index.php/ctl_dbcont/getmembersbybranch',
        success :   function(data){
                        $('#addreceiptddsmember').val(data);
                    }
        });
    }
</script>
<select id="addreceiptkccbranch" name="addreceiptkccbranch" onChange="kccbranchselect();" tabindex="1" >
    <option value="">--SELECT--</option>
    <?php foreach($branchlist as $value):?>
        <option value="<?=$value['branch_id']?>"><?=$value['branch_name']?></option>
    <?php endforeach; ?>
</select>
<select id="addreceiptddsmember" name="addreceiptddsmember" tabindex="1">
    <?php foreach($member_by_branch as $row) { ?>
        <option value = ""></option>
    <?php } ?>
</select>

my controller is 我的控制器是

function getmembersbybranch()
{
    $this->load->model('mod_user');
    $addreceiptkccbranchid      =   $_POST['addreceiptkccbranchid'];
    $data['member_by_branch']   =   $this->mod_user->member_receipt_dds($addreceiptkccbranchid);
    redirect('view_addreceipts');
}

i am generating a dropdown by selecting another dropdown option . 我通过选择另一个下拉选项生成一个下拉菜单。 . i cant access the controller method by putting url : '<?php echo base_url();?>index.php/ctl_dbcont/getmembersbybranch', in ajax ,why ?? 我无法通过在ajax中放置url : '<?php echo base_url();?>index.php/ctl_dbcont/getmembersbybranch',访问控制器方法url : '<?php echo base_url();?>index.php/ctl_dbcont/getmembersbybranch', ,为什么?

Here is a simple solution for this to work 这是一个可行的简单解决方案

AJAX Request AJAX请求

$.ajax({
    type : 'POST',
    data : 'addreceiptkccbranchid='+ $('#addreceiptkccbranch').val(),
    url : '<?php echo site_url("ctl_dbcont/getmembersbybranch");?>',
    success :   function(data){
                $('#addreceiptddsmember').val(data);
    }
});

Controller 控制者

function getmembersbybranch()
{
    $this->load->model('mod_user');
    $addreceiptkccbranchid      =   $_POST['addreceiptkccbranchid'];
    $data['member_by_branch']   =   $this->mod_user->member_receipt_dds($addreceiptkccbranchid);
    $this->load->view('member_by_branch',$data);
}   

View 视图

<?php
if($member_by_branch){
    foreach($branchlist as $value):
    ?>
    <option value="<?=$value['member_id']?>"><?=$value['member_name']?></option>
    <?php 
    endforeach;
}
?>

Redirect will not work. 重定向将不起作用。 Create a simple view for dropdown oprions. 为下拉选项创建一个简单的视图。

The redirect statement here is invalid, as it is an AJAX request. 此处的redirect语句无效,因为它是AJAX请求。 You have to send html or json response from server, which you can process at client side. 您必须从服务器发送html或json响应,您可以在客户端进行处理。 eg 例如

$data['member_by_branch']=$this->mod_user->member_receipt_dds($addreceiptkccbranchid);
echo $data['member_by_branch']; //assuming its html

so on client side, you just have to use this statment in you callback method. 因此在客户端,您只需在回调方法中使用此语句即可。

$('#addreceiptddsmember').html(data);

try this: 尝试这个:

Replace your js function by this function: 用以下函数替换您的js函数:

$("#addreceiptddsmember").live("change",function(){

   var htmlString="";
     $.ajax({
        type:'POST',
        data:'addreceiptkccbranchid='+ $('#addreceiptkccbranch').val(),
        url:'ctl_dbcont/getmembersbybranch',
        datatype:'application/json',
        success:function(data){
          $.each(data,function(i){
             htmlString+="<option  value='"+data[i].branch_id+"'>"+ data[i].branch_name +"</option>"
          });
          $('#addreceiptddsmember').html(htmlString);
     }
  });
});

in this you have to make htmlsting and append to selected list using jquery it won't be available in php foreach as you were doing using in the view code. 在这种情况下,您必须进行htmlsting并使用jquery附加到所选列表,因为您在视图代码中正在使用php foreach,因此它将不可用。

also remove 也删除

redirect('view_addreceipts');

& replace it by: 并替换为:

echo json_encode($data);
exit;

in the controller 在控制器中

hope it help! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM