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.


<html>
<head>
        <title>SAJAX Hello World</title>
        <script>
        < ?
        sajax_show_javascript();
        ?>

        function addString_cb() {
                x_stringCount(stringCount_cb);
        }

        function do_addString() {
                x_addString(document.getElementById('string').value,addString_cb);
                document.getElementById('string').value = "";
        }

        function addRandom_cb(result) {
                document.getElementById('canvas').innerHTML += '

'+result+'';
        }

        function stringCount_cb(result) {
                document.getElementById('count').innerHTML = result;
        }

        </script>

</head>
</html>

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

21 Responses to “AJAX Hello World with Sajax”

  1. 1

    [...] SAJAX inserito in php da fullo @ 11:57 joshua eichorn fa un esempio di come utilizzare le tecniche AJAX per realizzare un clas [...]

  2. 2

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

  3. 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. 4
    David Said:

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

    [...] This is a continuation of my AJAX Hello World series, in my earlier posts I covered sajax and JPSpan. In this article i’ll cover how to get a basic AJAX appliction up and running with HTML_AJAX. If you haven’t figured it out yet im one of the author’s of HTML_AJAX. The application in question is the same simple app as the other examples, it has an input box for adding random strings and an button to add a random one to a div. [...]

  8. 8

    Got SAJAX working

    Well I spent most of yesterday trying to get my nifty AJAX idea to work using JPSpan, I think I have everything right, but I still cant get it to work, I reckon I need to take it apart again…

  9. 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. 10

    SAJAX assists PHP, Io, Lua, Perl, Python and Ruby that’s great!

  11. 11

    hello , its nice script & SAJAX is a good ajax tools for programming .
    i use PHP and wotking with it !

  12. 12

    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 ?

  13. 13

    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.

  14. 14

    what is ajax? , how to use ajax?

  15. 15
    Mikael Andersson Said:

    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.

  16. 16

    [...] Sajaxhttp://www.modernmethod.com/sajax/http://blog.joshuaeichorn.com/archives/2005/04/19/ajax-hello-world-with-sajax/ [...]

  17. 17
    Mikael Andersson Said:

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

  18. 18

    It seems that something is wrong when I run the example with FF and IE!

  19. 19

    Yo: Its fixed now, the example was using short php tags < ? instead of the full style

  20. 20

    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

  21. 21

    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.


Leave a Reply

This circle expands additional navigation
You should really buy my AJAX book.
You'll learn not only the technology you need for implementation but also an understanding that will help you get the most from AJAX.
You'll also have my eternal thanks :-)