Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 putting url : '<?php echo base_url();?>index.php/ctl_dbcont/getmembersbybranch', in ajax ,why ??

share|improve this question
that not a controller but only a function inside class controller. – Kaii Apr 3 at 7:03
yeah its right . . how can i call this function from javascript @Kaii – Jay Apr 3 at 7:06
You are already calling it! But what is redirecting doing in ajax code? – itachi Apr 3 at 7:10
my problem is when i select an option from first select the onchange call javascript function and from this function i call controller at this time the controller function does not access – Jay Apr 3 at 7:38

3 Answers

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.

share|improve this answer
the second dropdown option is on same view on which first drop down,that means when i select a value from first drop down this page should load again. . . – Jay Apr 3 at 7:39
can i call same view in $this->load->view('//same view here',$data); or should i make a new view @raheel shan – Jay Apr 3 at 7:47
1  
this is nice answer but by url : '<?php echo site_url("ctl_dbcont/getmembersbybranch");?>',i can not go to getmembersbybranch – Jay Apr 3 at 7:56
well @Jay it was only example you can load same view and use controller method according to your requirement – raheel shan Apr 3 at 7:59
i want 'addreceiptkccbranchid='+ $('#addreceiptkccbranch').val(), send to url : '<?php echo site_url("ctl_dbcont/getmembersbybranch");?>', but not work . . – Jay Apr 3 at 10:14

The redirect 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);
share|improve this answer

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

also remove

redirect('view_addreceipts');

& replace it by:

echo json_encode($data);
exit;

in the controller

hope it help!

share|improve this answer
still i can't access the methoe from url:url:'ctl_dbcont/getmembersbybranch', – Jay Apr 3 at 10:11
ajax not work for me . . $('#addreceiptkccbranch').val(), this data does not pass to url:url:'ctl_dbcont/getmembersbybranch', – Jay Apr 3 at 10:16
@Jay I m sorry it was a typo now test this one and add alert("success") in the success function of ajax – sandip Apr 3 at 10:23
yeah alert works then why data is not pass to that method . . @sandip – Jay Apr 3 at 10:27
@Jay do you have firebug extension for firefox, check whether the data is getting passed or not, and if the data is not coming from the controller function then there is something wrong with database query etc – sandip Apr 3 at 10:30
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.