Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to refresh a php variable in view using Ajax?

I have this $unreadMessageCount variable in my view:

<a href="javascript:void(0)" id="myAccount" class="site-nav-sub"><i class="fa fa-angle-down site-nav-arrow">  </i>Hi, {{ firstname }} {% if unreadMessageCount > 0 %} <span class="badge pull-down" style="background: rgb(36, 232, 0); padding: 5px 1px;"> </span>{% endif %}</a>

In my ajax:

       window.setInterval(function(){
            $.ajax({ 
                url: '/index/retrieveUnreadCount',
                success: function(data){
                    console.log(data);
                }
            });
        }, 30000);

In my controller:

public function retrieveUnreadCount()
{
    if ($this->component->user->hasSession()) {
        $this->user = $this->component->user->getSessionUser();
        $this->view->setVar('user', $this->user);
        $this->view->setVar('unreadMessageCount', Messages::getTotalUnreadCount($this->user->id));

        $unreadMessageCount = Messages::getTotalUnreadCount($this->user->id);
        return json_encode($unreadMessageCount);
    }

}

I want to refresh $unreadMessageCount every 30 seconds using Ajax as seen above but I am not sure if this code works.



66.4k

Hi, i dont understand you complatly.

do you mean that

Messages::getTotalUnreadCount($this->user->id)

doesn't return the new value ?

or maybe you want to use the "new message count" in the page ?

sorry if i am stupid

edited Sep '15

Sorry, may be I didn't explain myself clearly. I got the value from the model and everything so it is fine. But then I want this variable to be refreshed in the html as shown in the first snippet every 30 seconds in view without refreshing the whole page

>Hi, >i dont understand you complatly. > >do you mean that > >```php >Messages::getTotalUnreadCount($this->user->id) >``` > >doesn't return the new value ? > >or maybe you want to use the "new message count" in the page ? > >sorry if i am stupid



66.4k
edited Sep '15

so in ...

public function retrieveUnreadCount()
....
return json_encode(['newMessages' => $unreadMessageCount]);

change html to :

<a href="javascript:void(0)" id="myAccount" class="site-nav-sub"><i class="fa fa-angle-down site-nav-arrow">  </i>Hi, {{ firstname }} <span id="user_messages_count">{% if unreadMessageCount > 0 %} <span class="badge pull-down" style="background: rgb(36, 232, 0); padding: 5px 1px;"> </span>{% endif %}</span></a>

and then the JS

window.setInterval(function(){
            $.ajax({ 
                url: '/index/retrieveUnreadCount',
                success: function(data){
                    if (data.newMessages > 0)  {
                        var $cont = $('#user_messages_count');

                        $cont.html( data.newMessages + '<span class="badge pull-down" style="background: rgb(36, 232, 0); padding: 5px 1px;"> </span>');
                    }
                }
            });
        }, 30000);

and it should work... however for those stuff i would suggest you to remove this mesasge count from being generated in the html at the start, so if you have to change something it will be only inside the javascript



7.3k
Accepted
answer

I see. Thanks alot. By the way do I need to disable view in controller?

>so in ... > > >```php >public function retrieveUnreadCount() >.... >return jsonencode(['newMessages' => $unreadMessageCount]); >``` > >change html to : > >``` ><a href="javascript:void(0)" id="myAccount" class="site-nav-sub"><i class="fa fa-angle-down site-nav-arrow"> </i>Hi, {{ firstname }} <span id="usermessages_count">{% if unreadMessageCount > 0 %} <span class="badge pull-down" style="background: rgb(36, 232, 0); padding: 5px 1px;"> </span>{% endif %}</span></a> >``` > >and then the JS > > >``` >window.setInterval(function(){ > $.ajax({ > url: '/index/retrieveUnreadCount', > success: function(data){ > if (data.newMessages > 0) { > var $cont = $('#user%5Fmessages%5Fcount'); >
> $cont.html( data.newMessages + '<span class="badge pull-down" style="background: rgb(36, 232, 0); padding: 5px 1px;"> </span>'); > } > } > }); > }, 30000); >
>``` > >and it should work... however for those stuff i would suggest you to remove this mesasge count from being generated in the html at the start, so if you have to change something it will be only inside the javascript



66.4k

try

echo json_encode(['newMessages' => $unreadMessageCount]);
exit;

if you are using phalcon 2.1 ->

echo json.encode...
return false;

if you are ising 2.0

check this one here:

https://github.com/ovr/phalcon-module-skeleton/tree/master/application/modules/api

otherwise i dont know :D