Onchange function using ajax in View |
Hi! I've an autocomplete input form with id 'poscustomer' . I want to display previous total balance whenever I select a customer.
I tried this but no results. would you please tell me which part of this code needs to be edited . Thanks controller function dispdata() { $poscustomer = $this->input->post('poscustomer',TRUE); $data['totalbalance'] =$this->pos_model->getBalance($poscustomer)->result(); echo json_encode($data); } Model public function getBalance($poscustomer) { $this->db->select('SUM(grand_total - paid) as totalbalance', false); $this->db->where('sales', array('customer' => $poscustomer)); return $result->totalbalance; } Javascript code in the view folder $(document).ready(function(){ $('#poscustomer').change(function(){ var poscustomer=$(this).val(); $.ajax({ url : "<?=admin_url('pos/dispdata');?>", type: "POST", data : {poscustomer: poscustomer}, cache: false, dataType: 'json', success: function(data){ //alert(data); $('#totalbalance').val(totalbalance); } }); }); });
Try this, you are returning an result object from your model.
PHP Code: echo json_encode($data, JSON_FORCE_OBJECT); PHP NET - json_encode Code: $('#totalbalance').val(data); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
What response are getting from the server? Check the network tab of your developers tools - any server side error should show up there.
It looks like you're on CI 3.x? If so, try changing this line:
Code: return $result->totalbalance; Code: return $this->db->get(); |
Welcome Guest, Not a member yet? Register Sign In |