1

I Am trying to make an ajax request from the view to controller, ajax requst is working fine, but from controller nothing is returned back to the view. i don't know where is the problem.. what i am trying is in my view side i am displaying some data from the controller and there is one select box. whe i select a city from the select box, it calls the ajax request and should show the result from that particular city in the 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 the 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 as $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 the controller

 function topdealajax() { $this->log('Ajax call -----------------'); if ($this->request->isAjax()) { $this->log('inside if request is 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'); } } } 
3
  • 1
    you shouldn't use exit, or break, you have to let it render and return the response, otherwise it doesn't return anything Commented Aug 28, 2013 at 11:53
  • 1
    same for $this->autoRender = false. If nothing gets redered, nothing gets returned! just $this->layout = null, and then normal data manipulation and rendering Commented Aug 28, 2013 at 11:55
  • * first of all your action in the controller is topdealajax() and you make an ajax request for topdeals() * second of all in the controller you set $data as $this->request->['city_id'] instead of $this->request->['Deal']['city_id'] Am I wrong? Why don't you sent the form normally and var_dump the POST in the action to see you are getting the desired values for the variables Commented Aug 31, 2013 at 13:29

1 Answer 1

0
/*IN THE AJAX REQUEST YOU SHOULD HAVE*/ $.ajax({ ..... success: function(data){ $('#MYDIV').html(data);}, //YOU CAN APPEND OR REPLACE THE CONTENT OF A CONTAINER WITH THE RESPONSE ... }); //in DealsController::topdeals() you should have at the begining if ($this->request->isAjax()): $this->layout = null; $this->view = 'view_ajax'; //Other view that doesn't needs layout, only if necessary endif; /*DO WHATEVER YOU WANT HERE, SEND IT TO THE VIEW, THE VIEW GETS RENDERD AND RETURN AS A RESULT*/ 
Sign up to request clarification or add additional context in comments.

5 Comments

$this->view = 'view_ajax'; means?
ii tried but still not getting..the result is not send back to the view , that is the problem. ajax request is working fine and in controller i am getting the id..
$this->view = 'view_ajax'; //means that you can have a different template for the ajax return if you look at the comments I made above at your question you will see that you have a rendering issue, because you break with exit and set autoRender to false, which means nothing gets returned
before you try to get the answer by AJAX you should first access the controller manually and dump some data, to see what happens
I edited my question please please check it and tell me whats wrong.. i struggled a lot for solving this problem from the last 2 days

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.