0

i need to fetch data from database from controller file and display into element file and ajax value showing in inspect element and also when i alert in ajax it is showing that value is going to controller.

but problem is that how can i echo or print ajax value in controller to fetch data from database and display it into element file?

how can i render custom element file in controller function?

ajax script

<script> $('#categories .accordion .tablist .tablistitem').on('click', function () { event.preventDefault(); $(".accordion li").removeClass("active"); var $li = $(this); $liid = $li.attr('id'); $slug = $li.data('slug'); $li.addClass("active"); $('#wrapper').append('<span id="load">LOADING...</span>'); $('#categories_info').show(); $.ajax({ type: 'POST', url: '/reviews/getsubcategories', data: {"selectid":$liid }, dataType:"text", success: function(data, textStatus, xhr) { alert(data); }, error: function(xhr, textStatus, error) { alert(textStatus); } }); }); </script> 

controller function

function getsubcategories() { echo $selectid= $_POST['selectid']; return $selectid; } 

element file

$SubCategoryObj = cri('Reviews'); $selectid = $SubCategoryObj->getMainCategories(); echo $selectid; 
2
  • which version of cake? Commented Apr 23, 2016 at 7:41
  • @JasonJoslin cakephp version is 2.0.13 Commented Apr 23, 2016 at 8:09

1 Answer 1

1

What you have done so far is mostly right, however in the past I have just created the view as normal in the View/Reviews folder.

In the controller set your data:

/app/Controller/ReviewsController.php

public function getsubcategories() { $this->layout = 'ajax'; $data = /**code to get data**/ $this->set('data', $data); } 

/app/View/Reviews/getsubcategories.ctp

<?php echo json_encode($data); ?> 

another option is to create the same view above but put it in file app/View/Ajax/json.ctp

And then inside the controller the last thing you call in the getsubcategories action is.

$this->render('Ajax/json');

In my experience elements are used inside views and not as replacements of views

Sign up to request clarification or add additional context in comments.

6 Comments

which part is not working? I've definitely managed to get this method working for me in the past.
function getsubcategories() { $data = "this is testing page"; $this->set('data', $data); } i have created getsubcategories.ctp and <?php echo json_encode($data); ?>
In your ajax method you have set dataType as text yet your data property is actually json. change your datatype to json. in your getsubcategories() action write debug($this->request->data);exit;. Do you get the ID you are posting in your alert? Also I like to use console.log() instead of alert () to debug my javascript. Can then see the output regardless of datatype in the browsers console tab of the inspector tools
and also how do i render ReviewsController.ctp in element file or controller?
If you can get that far i'm unsure how you cannot get to the view. Double check your folder structure and view name fit all the cake conventions and you shouldnt have a problem. Oh and 1 other thing. Should set $this->layout = 'ajax'. have updated my answer to match
|