Making jquery ajax call from view to controller in cakephp 2.x -
i trying make ajax request view controller, ajax requst working fine, controller nothing returned view. don't know problem.. trying in view side displaying data controller , there 1 select box. whe select city select box, calls ajax request , should show result particular city in view.ctp.
$('#cityid').change(function() { $city_id= $('#cityid :selected').val(); alert($city_id); $.ajax({ url : "<?php echo router::url(array('controller' => 'deals', 'action' =>'topdeals'), true); ?>", type : "post", cache : false, data : {city_id: city_id}, success : function(data){ alert(data); } }); }); });
and in view
<div id="form"> <?php echo $this->form->create('deal', array('action'=>'topdeals','type'=>'post'));?> <?php echo $this->form->input('city_id', array('label'=>'city','type'=>'select', 'id'=>'city_id','empty'=>'select city','options' =>$city)); echo $this->form->end(); ?> </div> <div class="line"></div> <?php if(!empty($topdealsortbyrank)) { foreach($topdealsortbyrank $topdealsortbyrank) {?> <div class="items"> <div class="itemslogo" > <?php echo $this->html->image('deal/dealimage/'.$topdealsortbyrank['deal']['image'],array('width'=>"100px",'height'=>"80px"));?> </div><!-- items logo ends--> <div class="itemdetails"> <b><?php echo $topdealsortbyrank['advertiser']['name']?></b> <p class="red"><?php echo $topdealsortbyrank['deal']['title']?></p> <?php } }?>
and in controller
function topdealajax() { $this->log('ajax call -----------------'); if ($this->request->isajax()) { $this->log('inside if request ajax -----------------'); $this->layout = null; $this->view = 'topdeals'; if(!empty($this->request->data)) { $this->log('inside if not empty of params -----------------'); $data = $this->request->data['city_id']; $this->log($data); $city_id=$data['city_id']; $this->log($city_id); $city_id= $this->request->data['city_id']; // $this->log($city_id); $topdealsortbyrank1=$this->deal->find('all', array('conditions'=>array('date_expiry >=' =>date('y-m-d ') , 'date_expiry <=' => 'date_expiry','deal.city_id'=>$city_id),'order'=>array('deal.deal_rank asc'))); //$this->log($topdealsortbyrank1); $this->set('topdealsortbyrank',$topdealsortbyrank1); $this->render('topdeals'); } } }
/*in ajax request should have*/ $.ajax({ ..... success: function(data){ $('#mydiv').html(data);}, //you can append or replace content of container response ... }); //in dealscontroller::topdeals() should have @ begining if ($this->request->isajax()): $this->layout = null; $this->view = 'view_ajax'; //other view doesn't needs layout, if necessary endif; /*do whatever want here, send view, view gets renderd , return result*/
Comments
Post a Comment