# Using Ajax to render elements

**URL:** <https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846>\
**Category:** Need Help\
**Created:** [January 8, 2017, 11:00pm UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846 "2017-01-08T23:00:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ginandjuice](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@ginandjuice](https://discourse.cakephp.org/u/ginandjuice)\
**Post date:** [January 8, 2017, 11:00pm UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/1 "2017-01-08T23:00:18Z")

</div>

Hello,  
I am developing a functionality for a blog, and I would like to send an ajax request. From debugging I can see my request gets sent to my controller ,and the SQL gets set, but I don’t see any response get set with ‘compact’.  
Can anyone help me? I don’t see any response.

My understanding is that from a request, cakephp will render, a view, and to have elements dynamically displayed, you need a null check if statement. Now I’ve done that, so to render that element I must set a variable with compact in the controller method. I’ve worked with ajax before with J2EE, but with cakephp I am not following its flow at all.

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [January 9, 2017, 3:28am UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/2 "2017-01-09T03:28:16Z")

</div>

First of all If you want answer which exactly solve your problem, you should share your code that you tried so far. Theoretical questions are not so much helpful for the others if some issues you are facing. Btw you can go through [http://stackoverflow.com/questions/30024565/simple-ajax-form-in-cakephp-3-0](http://stackoverflow.com/questions/30024565/simple-ajax-form-in-cakephp-3-0).

---

<div class="post-metadata">

**Author:** ![ginandjuice](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@ginandjuice](https://discourse.cakephp.org/u/ginandjuice)\
**Post date:** [January 9, 2017, 5:08am UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/3 "2017-01-09T05:08:05Z")

</div>

```
   if ($this->request->is('ajax')) {
          
        $some = TableRegistry::get('Articles');
        /* @var $z type */
        $d = $some->find('all')
                ->where(function ($exp, $q) {
                    return $exp->between('created', new DateTime(), new DateTime('2016-12-31'));
                })
                ->order(['created' => 'DESC'])
                ->limit(5);
        $this->set(compact('d'));
        $this->render('/Element/ajaxArticle');

```

Is my controller code, and in my view I have an element that is only displays if it is not null. I can see in my debug console that the variable is 1, when I use print\_r. But in the cake debug tool kit my variable never shows up, but I can see the query string when I look in the browser console.

Thank you

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [January 9, 2017, 5:14am UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/4 "2017-01-09T05:14:22Z")

</div>

Please follow this [https://book.cakephp.org/3.0/en/views/json-and-xml-views.html](https://book.cakephp.org/3.0/en/views/json-and-xml-views.html). You have to use `json_encode` then you will get the data in Ajax response.  
Also you can go through [http://stackoverflow.com/questions/38169410/cant-get-a-json-response-from-cakephp-3-controller](http://stackoverflow.com/questions/38169410/cant-get-a-json-response-from-cakephp-3-controller)

---

<div class="post-metadata">

**Author:** ![ginandjuice](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@ginandjuice](https://discourse.cakephp.org/u/ginandjuice)\
**Post date:** [January 14, 2017, 8:19pm UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/5 "2017-01-14T20:19:40Z")

</div>

Any reason why this database call would return an empty array? Something is still off with this.

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [January 16, 2017, 3:04am UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/6 "2017-01-16T03:04:02Z")

</div>

If database returning an empty array then there will be some issue with your data not with the Ajax. Try to debug your query separately and see what happens.

---

<div class="post-metadata">

**Author:** ![ginandjuice](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@ginandjuice](https://discourse.cakephp.org/u/ginandjuice)\
**Post date:** [January 16, 2017, 8:52pm UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/7 "2017-01-16T20:52:20Z")

</div>

I’ve debug my statement, and it says

> 2017-01-16 20:51:05 Debug: Cake\ORM\Query Object  
> (  
> [(help)] =\> This is a Query object, to get the results execute or iterate it.

There is more information. To me having this shows that I should be able to dynamically render elements in a view. But the ORM is not executing it seems?

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [January 17, 2017, 3:08am UTC](https://discourse.cakephp.org/t/using-ajax-to-render-elements/1846/8 "2017-01-17T03:08:44Z")

</div>

> [@ginandjuice](#):
>
> $this-\>set(compact(‘d’));

Try to use before set `$d = $d->toArray();`.
