0

My jquery alert is not working.I createtwo models- MedicalBillRecover BelongsTo MedicalBill MedicalBill hasMany MedicalBillRecover.

Now i have a onchange event in jquery ajax

$('#recover_billDate_id').on('change',function(){  
    var employee_id=$('#employee_recover_bill_id').val();
    var recover_billDate=$('#recover_billDate_id').val();

    var data = 'recover_billDate='+recover_billDate+'&employee_id='+employee_id;
    var url_to_call = '';

    url_to_call = '<?php echo $this->Html->url(array("controller" => "medical_bills","action" => "render_recover_total_balance")); ?>';

      $.ajax({
          type: "GET",
          url: url_to_call,
          dataType: "json",
          data: data,
          error:function(resp){
              alert('Oops..Something went wrong');

          },
          success: function(resp){           
           $.each(resp,function(index,obj){

          alert(obj.MedicalBillRecover.recovery_payment_amount);

            });             
          }

        }); 

});

Controller:

function render_recover_total_balance(){
      $this->autoRender = false;
      Configure::write("debug",0);
      $cond = '';

      if(isset($_GET['employee_id']) && trim($_GET['employee_id']) != '') {

        $employee_id = $_GET['employee_id'];
        $recover_billDate = $_GET['recover_billDate'];

        $cond['MedicalBill.employee_personal_id'] = $employee_id;
        $cond['MedicalBill.bill_date'] = $recover_billDate;
      }

      $employees = $this->MedicalBill->find('all',array('conditions'=>$cond));

      return json_encode($employees);
    }

jquery alert is not working. when i write url as-/gramin-vikash-bank-test/medical_bills/render_recover_total_balance?employee_id=1&recover_billDate=2014-04-22.

then i get result as

[{"MedicalBill":{"id":"2","employee_personal_id":"1","nature_of_disease":"er","bill_amount":"50000.00","bill_date":"2014-04-22","sanctioned_by_bank":"3.00","sanctioned_by_insurance_co":"3.00","temporary_advance_sanctioned":"100000.00","advance_date":"2014-04-21","ailing_person":"4"},"EmployeePersonal":{"id":"1",""category":"General", Nalbari","office_id":"1","department_id":"1","designation_id":"1","current_post":"","status":"1"},"MedicalBillRecover":[{"id":"1","medical_bill_id":"2","entry_date":"2014-04-22","recovery_payment_amount":"500.00","user_id":null},{"id":"2","medical_bill_id":"2","entry_date":"2014-04-23","recovery_payment_amount":"1000.00","user_id":null}]}]

Now how can i retrieve the MedicalBillRecover.recovery_payment_amount in jquery ajax alert.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2625357
  • 53
  • 1
  • 3
  • 12
  • 1
    It is good to use $this->request->query() rather then using $_GET... – Fazal Rasel Apr 23 '14 at 06:34
  • Do some debug actions: At first check the URL of your ajax-request (you can use Firefox + Firebug or IE by tab 'network'). At second, you can check/log your incoming data on server via `$this->log($this->request->query);`. The logged data will be written in `app/tmp/logs/error.log`. Use `tail -f error.log`. – Simon Apr 23 '14 at 06:59
  • this might help you- http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Fazal Rasel Apr 23 '14 at 07:42
  • Why don't you just write your url like: `/ControllerName/actionName` and since your are sending it as GET you need to create URL as get, with parameters. – skywalker Apr 23 '14 at 07:48
  • Is any error showing on console? – Fazal Rasel Apr 23 '14 at 08:43
  • In your controller you used `find('all')` but your `JSON` data indicating the result of `find('first')`...If its the result of `find('first')` then you can't use `$.each` on `jQuery`.. – Fazal Rasel Apr 23 '14 at 19:07

0 Answers0