0

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

4
  • that not a controller but only a function inside class controller. Commented Apr 3, 2013 at 7:03
  • yeah its right . . how can i call this function from javascript @Kaii Commented Apr 3, 2013 at 7:06
  • You are already calling it! But what is redirecting doing in ajax code? Commented Apr 3, 2013 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 Commented Apr 3, 2013 at 7:38

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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. . .
can i call same view in $this->load->view('//same view here',$data); or should i make a new view @raheel shan
this is nice answer but by url : '<?php echo site_url("ctl_dbcont/getmembersbybranch");?>',i can not go to getmembersbybranch
well @Jay it was only example you can load same view and use controller method according to your requirement
i want 'addreceiptkccbranchid='+ $('#addreceiptkccbranch').val(), send to url : '<?php echo site_url("ctl_dbcont/getmembersbybranch");?>', but not work . .
0

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

Comments

0

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!

5 Comments

still i can't access the methoe from url:url:'ctl_dbcont/getmembersbybranch',
ajax not work for me . . $('#addreceiptkccbranch').val(), this data does not pass to url:url:'ctl_dbcont/getmembersbybranch',
@Jay I m sorry it was a typo now test this one and add alert("success") in the success function of ajax
yeah alert works then why data is not pass to that method . . @sandip
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.