AJAX Hello World with Sajax

So in my last AJAX post I talked about a couple AJAX toolkits for PHP. Today I’ll cover a small example using SAJAX.

Setting up SAJAX

  1. Download SAJAX: http://www.modernmethod.com/sajax/download.phtml
  2. Copy Sajax.php from sajax-0.10/php to somewhere in your include path
  3. Your good to go.

Hello World php code

Now using AJAX for hello world is a bit over kill but we need an example so in this example will display a random string generated on the php side.

App php code

Just some simple functions to add strings to a session array, return its count, and to return a random string from it.


        if (!isset($_SESSION['strings'])) {
                $_SESSION['strings'] = array('Hello','World','Hello World');

        function addString($string) {
                $_SESSION['strings'][] = $string;
                return true;

        function randomString() {
                $index = rand(0,count($_SESSION['strings'])-1);
                return $_SESSION['strings'][$index];

        function stringCount() {
                return count($_SESSION['strings']);

SAJAX setup

Include Sajax.php and register functions that we want exported to javascript.


        //$sajax_debug_mode = 1;



The important thing to note is that SAJAX uses callback functions for return values so for most calls you’ll need 2 javascript functions. Otherwise its just simple DOM interaction in this example.

        SAJAX Hello World

View code of the entire script, view hello world example.

22 thoughts on “AJAX Hello World with Sajax”

  1. Pingback: Full(o)bloG » Blog Archive » SAJAX

  2. I saw the JPSpan example and is very good like this but sajax has the advantage that supports more languages.

  3. I tend to feel that the multi-language support in SAJAX is its biggest disadvantage. Its not like you’ll need to support multiple backend languages in 1 project and it leads to a lowest common denominator api that isn’t great for any of the languages it supports.

  4. Wow… this is a really nice way to explain SAJAX. Thanks for going “over kill” to explain this. It really helped me and I know I will be trying to implement (S)AJAX into my website!

  5. I am interested in adding Ajax to our website. We are using a linux server. I want to handle the requests inside a C library I am developing. Is there any documentation available that shows how to interface with the httprequest?

  6. httprequest makes a request against your web server that is the same as anything else. If you have a C library you’ll either need to check out howto do cgi in C or howto make it either an apache or php extension.

  7. Pingback: There and Back Again » Blog Archive » AJAX Hello World with HTML_AJAX

  8. Pingback: Geffy & The Ninja Within

  9. Only 1/09 I find Sajax. And after 3-4 minutes of viewing examples & FAQ, I understand: it’s veeeery nice project. I like it! 😉

    And I try use him in my future project’s. Thank you for this useful information!

  10. I’m a newbie in using ajax or sajax, I didn’t get this running with,and I’m quite confused why in your example you have this javascript functions “stringcount_cb” , when the function that you had on your php script is “stringcount”.. so does “cb? have something to do with sajax? how does this go ?

  11. mabus:
    cb stands for callback, its just a naming convention, when you make a call with sajax you pass in the function to call with the return value.

  12. Hi my name is Mikael Andersson

    I have used sajax for a while. I have one problem.

    I send a filename as a value to php with sajax.
    But somewhere it’s changing my values.

  13. Pingback: Aprendiendo AJAX - Angel "Java" Lopez

  14. The charset must be set in the header. iso 8859-1 for swedish language.

  15. Hi joshua,

    I have this problem: i can set up the Sajax examples easily, but i cant find anywhere how i should connect and fetch from the database with mysql.
    If i make a normal function which gets information from the database, it works. If i use that same content of that function and place it in a Sajax php function, it doesnt. You have any idea why? Or how to work with databases with sajax?

    thanks in advance,

  16. Danny: You shouldn’t have any problem connecting to the db with sajax its just a normal web request to php. Just make sure the function works properly when called from a test script.

  17. Hi i just wanted to note that Sajax has moved to sajax.info, there you can get the latest version that has more function and all around better support of standards and browsers. Hope to see you there.

Comments are closed.