CodeIgniter Forums
Onchange function using ajax in View - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Onchange function using ajax in View (/showthread.php?tid=76193)



Onchange function using ajax in View - jordi_1h - 04-20-2020

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


RE: Onchange function using ajax in View - InsiteFX - 04-21-2020

Try this, you are returning an result object from your model.

PHP Code:
echo json_encode($dataJSON_FORCE_OBJECT); 

PHP NET - json_encode

Code:
$('#totalbalance').val(data);



RE: Onchange function using ajax in View - jordi_1h - 04-21-2020

thanks a lot for your reply . I tried your suggestion the browser shows no error but does not display the result


RE: Onchange function using ajax in View - maxxd - 04-25-2020

What response are getting from the server? Check the network tab of your developers tools - any server side error should show up there.


RE: Onchange function using ajax in View - jordi_1h - 04-26-2020

Thanks for your suggestion. It is displaying {totalbalance: null}
totalbalance: null


RE: Onchange function using ajax in View - maxxd - 04-26-2020

(04-26-2020, 07:05 PM)jordi_1h Wrote: Thanks for your suggestion. It is displaying {totalbalance: null}
totalbalance: null

That would explain why it's not showing anything. Perhaps check the getBalance() method as $result doesn't appear to be defined before you try to return it.


RE: Onchange function using ajax in View - jordi_1h - 04-28-2020

(04-26-2020, 09:13 PM)maxxd Wrote: That would explain why it's not showing anything. Perhaps check the getBalance() method as $result doesn't appear to be defined before you try to return it.
Sorry, I tried everything without success. Any other suggestion? thanks


RE: Onchange function using ajax in View - maxxd - 05-01-2020

It looks like you're on CI 3.x? If so, try changing this line:
Code:
return $result->totalbalance;
to
Code:
return $this->db->get();



RE: Onchange function using ajax in View - jordi_1h - 05-01-2020

Thanks a lot for your suggestions . Now It is returning false value