Reputation: 402
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 ??
Upvotes: 0
Views: 10200
Reputation: 3289
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!
Upvotes: 0
Reputation: 19882
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.
Upvotes: 1
Reputation: 19228
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);
Upvotes: 0