The new way to write AJAX applications with PHP

Write Ajax applications in theses day are a common thing, the “day by day” of the web developer. Sometimes, especially for complex things, write ajax is not so easy, because you need to know the client side (javascript) and the server side. The dream of every php developer (at least mine dream :-) ) is to write ajax without need to carry about the javascript code.

Even there is a lot of ajax framework that minimize what the php developer need to know about javascript, sometimes their very hard to use (a lot of php code).

I’ve wrote the Auto Ajax, which update only parts of pages when links are clicked using AJAX to avoid full page reloading. It uses some Javascript code to intercept the clicks on the current page links. The link clicks trigger AJAX POST requests to fetch the content to update specific page sections, instead of the whole page. If the browser does not support Javascript or AJAX requests, the clicks on the links will make the browser retrieve the whole page as usual.

Most of time, ajax is not for download pages only, they’re used to process forms, download a new fraction of the page, more interaction with the user, and others.

In the bus, coming from work to home, I was thinking (the best to think), trying to figuring out how to build an ajax framework, easy to use, powerful, and avoiding javascript as much as possible for the final user (the php developer).

The I had an idea(I don’t if it is the best), write ajax application defining an php class, with two or three methods. Then there was born the PHP-AJAX project The result of the idea is explained bellow:

For write a new ajax application, you need to extend the phpajax class, then override the method “main”, then write the code. Additionally you can override the method “input” and there define all the inputs to read from the client, and “loading” for do something while the ajax request is in process.

class example1 extends phpajax {
/**
* Input
*
* Read all what is needed from client.
*
*/
function input() {
aread(“input1″);
aread(“input2″);
}

function loading() {
aprint(‘loading’, ‘Loading…’);
ashow(‘loading’);
}

function main() {
/*
* The variables that was read in the method
* “input” is referenced here as local variable
* for made easy in the code time.
*/
$a = & $this->input1;
$b = & $this->input2;

/* Sleep awhile for show the loading */
sleep(10);

if ( !is_numeric($a) ) {
alert(“A ($a) must be numeric”);
ahide(“output1″);
/* stop ajax */
return;
}

if ( !is_numeric($b) ) {
alert(“A ($b) must be numeric”);
ahide(“output1″);
/* stop ajax */
return;
}

aprint(‘output1′, $a*$b);
ahide(“ashow”);

}
}

For calling the ajax application defined above, you need to call a javascript function with the same of the ajax-controller class.

<?php phpajax_js(“./”);?>
A = <input type=”text” name=”input1″ id=”input1″><br/>
B = <input type=”text” name=”input2″ id=”input2″><br/>
A * B = <span id=”output1″></span><br/>
<input type=”submit” value=”Click me” onclick=”example1()”>

PHP-AJAX is near of a release, the project will be hosted in PHPClasses because it offer a system with forums, and let me send emails to peoples who download the class advising of new changes.

BTW, you can see the PHP-AJAX source code on the project repository.

17 Comments

  1. Comment by Virgile on November 27, 2007 7:01 am

    It really looks like xajax (http://www.xajaxproject.org/). Can you expose some differences?

  2. Comment by Lyubomir Petrov on November 27, 2007 7:45 am

    Some one here heard about XOAD ?

  3. Comment by Nikola on November 27, 2007 9:20 am

    Bei all means people, this is JavaScript from the web 1.0 era - obtrusive and global.
    A nice article from Roger has been published today.

  4. Comment by Cesar D. Rodas on November 28, 2007 1:27 am

    basically the diference is that you must think write class instead of only call back functions.

    and i am developing new things such as upload things with ajax, that will be easy to code for the programmer.

  5. Comment by Cesar D. Rodas on November 28, 2007 1:28 am

    and I know XOAD

  6. Comment by Michal Slaby on November 28, 2007 8:48 am

    Looks like very, very crippled variation of the idea introduced by Tigermouse framework - see http://tigermouse.epsi.pl

  7. Comment by Pablito on December 4, 2007 4:41 pm

    Its works, its simple to use and fairly simple to implement… no critics so far :)

  8. Comment by George on December 10, 2007 7:50 pm

    Seems to work okay with tables, but when put in a tableless test page (3-col w/footer and header, div css controlled) pages refresh back to default with time update. Removed all other js from the page, same problem.

  9. Comment by Shailesh on January 4, 2008 10:17 am

    It works the way it should be with no javascript requirement.
    But , when i tried with mozilla thunderbird, it was not able to read the values from textbox.
    So, it has a bug.

  10. Comment by Shailesh on January 4, 2008 10:18 am

    oh o, sorry for the last post.
    its mozilla firefox.

  11. Comment by Cesar D. Rodas on January 4, 2008 1:44 pm

    Really, can you post here more details? or please put on the php class forum, http://www.phpclasses.org/discuss/package/4295/

  12. Comment by Shailesh on January 6, 2008 3:10 am

    Hey it worked.
    For mozilla firefox you need to give id.
    and it works.
    Well, can i pass the arguments to main class directly.
    I have given a link on my page for the user to delete the item. I want to pass a itemid for deletion. How can i do this?
    Any suggestion.
    Thanks

  13. Comment by Cesar D. Rodas on January 6, 2008 3:42 am

    When you define a class for handle an ajax request, then you can:

    class foo extends phpajax {
    function inputs() {
    aread(”input1″);
    aread(”input2″);
    }

    }

    Then in your HTML you pass paremeters like this:

    click me!

    And “input1″ will have the value of one and “input2″ the value of 2.

    So, you can pass parameters as the javascript function parameter, or it will search HTML objects (input1, input2 in the example) and get its value.

  14. Comment by Shailesh on January 23, 2008 8:15 am

    Hi
    thanks Cesar.
    Can i someway read a array (local array in php page) in phpajax main function. and if yes how to do that?

    Thanks

  15. Comment by Bernhard on April 2, 2008 12:04 pm

    Hello Cesar,
    as i get started with AJAX, should i use AutoAjax or your other class PHP Ajax?

    I can’t see the difference between them. Maybe AutoAjax is just a newer and better version of PHP Ajax?

    Bye

    Bernhard

  16. Comment by Jacek on May 20, 2008 5:27 pm

    Hi,

    What’s the status of the project? It was supposed to be released few months ago. Is it still developed?

  17. Comment by orliandro on July 24, 2008 8:28 pm

    hi excelent work… but.. i have a BIG PROBLEM!…

    i want to use letters with (acento) áéíóú and letter ñ Ñ

    whats is the solucion to resolve this bug?
    exist?

    viva méxico cabrones!!! :)

Comments RSS TrackBack Identifier URI

Leave a comment