How To Use SAJAX (AJAX Framework) from HTML

By Angsuman Chakraborty, Gaea News Network
Monday, May 15, 2006

Sajax is an useful ajax framework to simplify ajax based development for languages like PHP, Ruby etc. Not many realize Sajax can be used equally well from plain old html pages (as in .html or .htm). Their website doesn’t mention it anywhere so I can guess it was not one of their use cases. However sajax is based upon ajax which is plain old Javascript. Let’s find out how we can use trusty old sajax to liven up your static pages.

To use sajax from html you need to get the javascript code it generates and include it in your html pages.
Sajax doesn’t tell you how you can create a javascript file to include (in your html pages), thereby allowing you to call php (for example) function calls from client side javascript. This can however be remedied with a simple call:
<?php file_put_contents(’filename.js’, sajax_get_javascript()); ?>

This generates the required javascript file (change filename to your liking). Include this file in your html pages and you are good to go. Now you can call your exported php function from javascript. Remember to append the function names with x_.

How can you use it?
Check my Annotated wordPress source code cross-reference site for an example. Check out the sajax based commenting system.

PS. As a side tip you should know sajax_init() function call is not required and can be eliminated with current versions of Sajax. It does nothing.

Discussion

Chris Hobbs
September 9, 2007: 9:50 am

Your site says “Remember to append the function names with x_.” In fact, in my experience the “x_” needs to be prepended, not appended.

January 14, 2007: 3:55 am

[...] There is a third and even simpler way to execute programs and display the results using AJAX. You can use any popular AJAX framework like Sajax or Prototype to make calls to the server and then display the results dynamically. This had the added advantage of being able to repeatedly update page contents. [...]

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :