Lightweight Ajax framework for PHP.

Discussion in 'PHP' started by blueparukia, Sep 10, 2011.

  1. #1
    Hello all,

    I am currently writing a very comprehensive system which aims to have concise, optimized code and still be very easy to learn use for people contributing code.
    Unfortunately I am now up to the Ajax portion of the system. Ajax is something I've used only a few times before several years ago and I'm not sure of the optimum approach, nor do I really have the time to learn since I'd like to integrate Ajax features now in the early development stages of the project as opposed to tacking them on at the end or halfway through and having to rewrite alot of my PHP.

    So I am after a framework or toolkit that can call PHP functions from the client and use as little footprint as possible. The two I am looking at are Sajax and Xajax.

    Sajax I have used before, but a long time ago and I am looking at it now and thinking it seems unnecessary to have 4 JavaScript files, although the code seems to be written at least reasonably well so is my first preference.
    Xajax again looks alright but seems to be an entire library of PHP files which is unnecessary, more than 1 or 2 is too much to integrate into a system.

    So I just wondering if anyone had any suggestions? I am browsing through a list now and looking at examples but first hand experience is always a better recommendation.

    I am proficient in PHP and JavaScript.

    Thank you for your time.
     
    Solved! View solution.
    blueparukia, Sep 10, 2011 IP
  2. #2
    Sajax and Xajax have a large footprint in the code you write, jQuery has a much smaller footprint in what you write.

    
    
    $.ajax({
    
       url: "test.html",
       context: document.body,
       success: function(data){
         alert(data);
       }
     });
    
    Code (markup):
    is about it. The last line will be different, depending on what you want to do with the returned data. It's also pretty trivial to use JSON data.

    If you don't want the overhead of any library:

    
    function GetXmlHttpObject() {
        var xmlHttp=null;
        try {
            [COLOR=#00ff00]// Firefox, Opera 8.0+, Safari[/COLOR]
            xmlHttp=new XMLHttpRequest();
        }
        catch (e) {
            [COLOR=#00ff00]// Internet Explorer[/COLOR]
            try {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
    
    function ajaxCall(args) {
        xmlHttp=GetXmlHttpObject();
    
        if (xmlHttp==null) {
            alert ("Your browser does not support AJAX!");
            return false;
        } 
    
        var url="yourPhpFile.php";
        url=url+"?GETArgOne="+someValue;
        url=url+"&GETArgTwo="+anotherValue;
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
        return false;
    } 
    
    function stateChanged() {
        if (xmlHttp.readyState==4) { 
            if (xmlHttp.status == 200) {
                resp = xmlHttp.responseText;
                [COLOR=#00ff00]//resp is the data sent back by the PHP file[/COLOR]
            } else {
                document.getElementById('display').innerHTML =  'An error (' + xmlHttp.status + ') occurred: ' + xmlHttp.statusText;
            }
        }
    }
    
    
    
    Code (markup):
    is all you need to do AJAX.
     
    Rukbat, Sep 11, 2011 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    jQuery or prototype or mootools.
     
    jestep, Sep 13, 2011 IP
  4. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #4
    JohnnySchultz, Sep 14, 2011 IP
  5. salsan

    salsan Active Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #5
    Try sAjax... it is a very simple & easy to use ajax framework...
     
    salsan, Sep 14, 2011 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Thanks I was able to construct my own, which functions similar to sajax but I think a little easier to use and better suited to my project.

    Thank you.
     
    blueparukia, Sep 15, 2011 IP
  7. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #7
    jquery is the best framework....
     
    salmanshafiq, Sep 16, 2011 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    jQuery is the easiest framework. If you want the lightest one, writing the code by hand is much lighter. "Best" depends on what you want. If I don't care about weight, or I need jQeury for something else (it's hard to beat jqGrid with anything lighter), I'll use jQuery's ajax(). But if I need something small and fast-loading, I'll just pop in the code manually. It's small, light and fast. Sure a semi has a better engine than a Yugo, but would you use one to drive yourself around the corner?
     
    Rukbat, Sep 16, 2011 IP
  9. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #9
    The - SECOND - AJAX code provided by rukbat is the easiest and possibly most reliable AJAX code.
    What that code is , is an engine that's already set up to handle the variables form any PHP handlers or process of handlers are how ever large you make them , example you could have 100,00 libe sof If/Else if and output the data at the end to the $var that AJAX will display

    The Ajax Engine overview
    document.getElementById is the end source that displays whatever you feed into it , and it displays it to the <DIV> table by element id ..
    your element ID can be a value coming off dropdown boxes, text boxes, variables present on the page form a database etc. As long as you give the ElementId the value name.
    At the end of that you call the PHP handler file (includes php file handler.php) the values of the DHTML /javascript values will be sent through the $_GET function instead of $_POST , the PHP handler should receive these variables as $_GET
    (example:
    $tshirt_color1 = $_GET['tshirtcolor1'];
    $tshirt_color2 = $_GET['tshirtcolor2']; etc.
    The PHP handler catches those variables coming in it processes them however you want, example: say it grabs the pricing from an SQL database. and comes back with pricing size , available stock. So you process all that PHP function on your handler ..and then output the last code to be displayed inside the <DIV>table

    When you click your example Green T-shirt button on a tshirt site , the values go out as $_GET, they are picked up by PHP handler, it processes the PHP functions,and spits out the results example:
    $price = $result1;
    $stock_avail =$result2

    etc. The results are displayed in the <div> as fast as they are processed by PHP .

    That engine is the most reliable, and there are at least 3 or 4 variables to watch for in that AJAX engine you must update for your own use.The URL thats being sent, you may even concatenate to send more vars that one at a time. (which will be your next question once you realize you need to send more than one var at a time) :D
     
    ezprint2008, Sep 17, 2011 IP