There and Back Again

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.

<?php

        session_start();

        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.

<?php

       require("Sajax.php");

        sajax_init();
        //$sajax_debug_mode = 1;
        sajax_export("addString");
        sajax_export("randomString");
        sajax_export("stringCount");

        sajax_handle_client_request();

?>

HTML Code

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.

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. imcsk8

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

  3. Joshua Eichorn Post author

    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. David

    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. Jay Potter

    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. Joshua Eichorn Post author

    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. Vlad

    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. mabus

    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. Joshua Eichorn Post author

    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. Mikael Andersson

    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. Mikael Andersson

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

  15. Danny

    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,
    Danny

  16. Joshua Eichorn Post author

    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. AJenbo

    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.