Store
Community Documentation

v3 Knowledgebase

Mini-Tut: Basics of phpFoX AJAX

So I was just messing around with the phpFoX codes, and am trying to figure out the Ajax system, I've got the basics down and would like to share what I've learned; hopefully anyone else who has experience in the phpFoX ajax system we can add to this thread since ajax is what drives most sites these days.

Now, in this mini-tutorial we'll be making a simple "Hello World" program using Ajax, for no particular reason I added this to the forum module, so that's the example I'll be using.

Files we'll be editing:
  1. module/forum/template/default/controller/thread.html.php


So the ajax.class.php holds all the ajax functions that interact with the forum part of the site, now to put it simply ajax is what interacts client side with server side without page reloading, it runs in the background.

So open up ajax.class.php, and in the bottom of the file or the top (doesn't matter just make sure it's not in other method brackets) add something like:

PHP:
public function helloWorld()
    {
       
Phpfox::isUser(true);
       
$this->alert('Hello World and hello, ' Phpfox::getUserBy('user_name'));
    } 


So here we're declaring a public function/method in the class which will make an alert box using thickbox saying "Hello World and hello, {persons username here}" You'll also notice we've added a line of code "Phpfox::isUser(true)" which; if they're not logged in should redirect them to a login page, most all the AJAX functions have this bit of code to make sure logged out people can't use them, so it's good practice to start out with that line of protection. Also another method we're calling is "getUserBy" which will naturally return the persons username.

Now open up thread.html.php, and we are going to call the ajax function and get our results, find this piece of code:

HTML:
<input type="button" value="Go Advanced" class="button" onclick="Phpfox.forum.goAdvanced();" />


Now after this let's add our link to call the ajax function:

HTML:
- <a href="#" onclick="$(this).ajaxCall('forum.helloWorld'); return false;">Hello World</a>


So the link doesn't go anywhere and return false, but it calls the ajax function for us in this format "ajaxCall('MODULE.FUNCTION_NAME')"

This little hello world program can be viewed on a thread, next to the go advanced button. People with good PHP knowledge and Javascript knowledge can expand on this and make nice mods, addons, etc using AJAX the step forward. If any more experienced folks have some tips or more beneficial info please add teeth