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
- Download SAJAX: http://www.modernmethod.com/sajax/download.phtml
- Copy Sajax.php from sajax-0.10/php to somewhere in your include path
- 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>






[...] SAJAX inserito in php da fullo @ 11:57 joshua eichorn fa un esempio di come utilizzare le tecniche AJAX per realizzare un clas [...]
I saw the JPSpan example and is very good like this but sajax has the advantage that supports more languages.
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.
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!
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?
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.
[...] 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. [...]
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…
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!
SAJAX assists PHP, Io, Lua, Perl, Python and Ruby that’s great!
hello , its nice script & SAJAX is a good ajax tools for programming .
i use PHP and wotking with it !
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 ?
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.
what is ajax? , how to use ajax?
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.
[...] Sajaxhttp://www.modernmethod.com/sajax/http://blog.joshuaeichorn.com/archives/2005/04/19/ajax-hello-world-with-sajax/ [...]
The charset must be set in the header. iso 8859-1 for swedish language.
It seems that something is wrong when I run the example with FF and IE!
Yo: Its fixed now, the example was using short php tags < ? instead of the full style
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
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.