Welcome Guest, Not a member yet? Register   Sign In
Onchange function using ajax in View
#1

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);
}
});
});
});
Reply
#2

(This post was last modified: 04-21-2020, 03:21 AM by InsiteFX.)

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);
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 04-22-2020, 09:30 AM by jreklund.)

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

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

(This post was last modified: 04-26-2020, 07:34 PM by jordi_1h.)

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

(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.
Reply
#7

(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
Reply
#8

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();
Reply
#9

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




Theme © iAndrew 2016 - Forum software by © MyBB