MyBB Community Forums

Full Version: Ajax to refresh posts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I was looking through the plugin requests forum when I saw an idea that I thought would be pretty good, refreshing the posts at a set time via AJAX.

I saw Euantor say:

Quote:This would require a pretty simple plugin hooking into xmlhttp and a little javascript. Would be fairly easy for somebody to do.

What sort of thing would I use for the javascript? I'm not too sure how the posts are loaded.

Thanks Smile
Well first of all you'd need to create a function hooked into xmlhttp_start (I baelieve that's the hook off the top of my head) to get the newest posts from a certain thread then you could do this via JS to get the actual posts and store them in a variable:

//you'd need to define the TID in the showthread template - something like this:
var tid = {$thread['tid']}

// then use this in a javascript file
new Ajax.Request('xmlhttp.php?action=getnewposts&tid=' + tid, {
    method: 'get',
    onSuccess: function(transport) {
        // transport holds the response from the call. Best thing to do while developing would be to console.log(transport); to see the actual contents of the response so you know how to handle it
    }
});

That ought to help you. It's been a while since I've used Prototype to run any kind of AJAX stuff before seeing as I tend to stick to jQuery.
Thanks for the reply, Euan

So in the showthread template:
<script type="text/javascript">
var tid = {$thread['tid']}
</script>

Then I'm using the hook:
$plugins->add_hook("xmlhttp", "refresh_posts");

Would I put:
new Ajax.Request('xmlhttp.php?action=getnewposts&tid=' + tid, {
    method: 'get',
    onSuccess: function(transport) {
        // transport holds the response from the call. Best thing to do while developing would be to console.log(transport); to see the actual contents of the response so you know how to handle it
    }
}); 
in the headerinclude template?

What sort of thing would need to be added in the refresh_posts function?

Thanks again! Smile
Yes, that's right, but you'll need to ensure that the variable for tid is populated before the script in the headerinclude.

In the refresh_posts function you'd obviously need to grab the tid input then get the unread new posts from that tid and then echo them out ready in the psotbit template. Here's some pseudo code:

function()
{
    $tid = (int) $mybb->input['tid'];

    $posts = get_unread_posts($tid);

    foreach I$posts as $post)
    {
        $postbits .= build_postbit($post);
    }
    
    echo $postbits;
}
Cheers Euan,

I've currently got:
function refresh_posts()
{
$tid = (int) $mybb->input['tid'];

    $posts = get_unread_posts($tid);

    foreach ($posts as $post)
    {
        $postbits .= build_postbit($post);
    }
    
    echo $postbits;
}

Then in my showthread template I put:
<script type="text/javascript">
var tid = {$thread['tid']}
new Ajax.Request('xmlhttp.php?action=getnewposts&tid=' + tid, {
    method: 'get',
    onSuccess: function(transport) {
        // transport holds the response from the call. Best thing to do while developing would be to console.log(transport); to see the actual contents of the response so you know how to handle it
    }
});
</script>

Is this correct as they still don't seem to be refreshing

Thanks again, +Rep Smile
As I said, what I posted was just pseudocode. I don't believe there's even such a function as get_unread_posts() - I just don't have time to write the full code to do it ;P As for the AJAX, it's right enough but obviously you'll want to run it at a set interval using something like window.setInterval().
Thanks Euan Smile