php - call controller from javascript on view using codeigniter
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 ??
Answer
Solution:
Here is a simple solution for this to work
AJAX Request
Controller
View
Redirect will not work. Create a simple view for dropdown oprions.
Answer
Solution:
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.so on client side, you just have to use this statment in you callback method.
Answer
Solution:
try this:
Replace your js function by this function:
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
& replace it by:
in the controller
hope it help!