Autoajax
Docs > Extensions > Plugins > AutoajaxThe AutoAjax function is a helper script which makes it easier to create ajax based scripts that automatically update your site. This can create some beautiful effects, but you do need some additional pieces to make it work. Here's an intro to how you could add a "like" feature for users to thumbs up specific comments.
First, enable AutoAjax in your site.config file. If you only want this in specific sections of your site, specify those folders. Otherwise, set it for your entire site:
enableAutoAjax
enableAutoAjax: forum*,blog*
Now to make it work you need a few additional pieces. I recommend putting these in a separate plugin (such as "like.php") so the ajax auto can be used separately for other ajax processes if desired. Don't forget to enable this other plugin as well!
This second plugin will need at least two functions:
1) One function is used to set up the ajax connection. Basically, this function needs to call the "loadAuto" function with a specified ajax process name, and output a link with a specific bit of javascript. In this example, my ajax process is called "like".
function BOLTFlike($args) {
global $fieldURL;
loadAuto('like'); // this sets the ajax process name
return BOLTescape("<a href="javascript:void(0);" onclick="BOLTauto('$args[1]');"><img src=$fieldURL/files/like.jpg></a>");
}
global $fieldURL;
loadAuto('like'); // this sets the ajax process name
return BOLTescape("<a href="javascript:void(0);" onclick="BOLTauto('$args[1]');"><img src=$fieldURL/files/like.jpg></a>");
}
In this case, I use the $args[1] parameter to pass the page and comment parameters in the ajax call, by putting this in my page: [(like 'comment={+p}&page={p}')]. Note that I'm using a query to generate the comments display, making {+p} the comment id.
2) The other function does some action triggered by this ajax call. It involves creating a page in the code.auto page hierarchy, with the name of your ajax process. In this example, I create a page called code.auto.like and put one line on it: <(autolike)>, but it could include additional lines if desired. This autolike function simply increments the like count of the specified comment on the specified page. It also returns a simple bit of text like "SUCCESS" to indicate the page has been updated, and then exits php to stop any further BoltWire processing.
function BOLTFautolike($args) {
$page = $_GET['page'];
$comment = $_GET['comment'];
$count = BOLTinfoPeek("$page:likes::$comment") + 1;
BOLTinfoPoke("$page:likes", $comment, $count, '', false);
print_r('SUCCESS');
exit;
}
$page = $_GET['page'];
$comment = $_GET['comment'];
$count = BOLTinfoPeek("$page:likes::$comment") + 1;
BOLTinfoPoke("$page:likes", $comment, $count, '', false);
print_r('SUCCESS');
exit;
}
Once "SUCCESS" is returned, it triggers the ajax script to reload the page and the new count will be display on the current page view.
This is a very simple script and could be made as fancy or as complex as desired. In my script for example, I pass the id of the person liking the comment and save that as well, and use that to ensure they can only like each comment a single time. But those kinds of enhancements can be managed easily enough within BoltWire once you understand how to use this script.