php - Cakephp fetch data from database without refreshing the page -


i working on cakephp 2.x ....actually want showing battery level on view page , memory usage too... battery , memory keeps on changing after seconds or minutes ... dont want user refresh or reload page evry time , checks status of these 2 ... want data db in ajax or jquery , show them user ... know syntax of sending form data , return in ajax ... here not sending .. there other things on page in need data in ajax ... me ... if has implemented before please share

in controller logic , return ajaxresponse.

  class foocontroller extends appcontroller{     public $components = array("requesthandler");     //... other actions here      public function getsysparams(){            if($this->requesthandler->isajax()){              //your logic here, example:              $sysinfo = $this->foo->find('first', array('fields'=>array('battery', 'cpu'));              //return data ajax call using json_encode              return new cakeresponse(array('type' => 'application/json',                 'body' => json_encode(array('battery' => $sysinfo['foo']['battery'], 'cpuusage'=>$sysinfo['foo']['cpu']),                 json_force_object),                 'charset' => 'utf-8')              );           }      }    } 

and in js come this:

{'battery':"33%", 'cpuusage':"80%"} 

i've tested on cakephp 2.3 , works. on previous versions i'm not sure.


edit

this javascript part using jquery:

       $.ajax({          url: '/foo/getsysparams'          data: {'foo': 'bar'}, //any data want send          success: function(datareturned){              $("div#update").text(datareturned.battery);              $("div#update2").text(datareturned.cpuusage);          }        }); 

Comments