php - call controller from javascript on view using codeigniter

582

hey guys i am facing trouble in calling controller method from 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 puttingurl : '<?php echo base_url();?>index.php/ctl_dbcont/getmembersbybranch', in ajax ,why ??

563

Answer

Solution:

Here is a simple solution for this to work

AJAX Request

$.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.

368

Answer

Solution:

Theredirect statement here is invalid, as it is an AJAX request. You have to send html or json response from server, which you can process at client side. e.g.

$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);
714

Answer

Solution:

try this:

Replace your js function by this function:

$("#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 makehtmlsting and append to selected list using jquery it won't be available in php foreach as you were doing using in the view code.

also remove

redirect('view_addreceipts');

& replace it by:

echo json_encode($data);
exit;

in the controller

hope it help!

People are also looking for solutions to the problem: php - Symfony / Doctrine "refers to the owning side field which does not exist" - but property is present in class

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.