AJAX Question: How do you pass your data?

Discussion in 'PHP' started by crath, Jul 22, 2009.

  1. #1
    I am just getting into creating advanced system with php/ajax and it seems like my method of passing arrays of data from PHP to JS is inefficient, so I would like to know what you guys suggest? Here are some of the options I can think of:


    a) Pass my data as XML, and use JS DOM functions to pick apart the data

    b) Pass my data as a string, and break it apart using unique breakers such as &*~*& and then split the strings at those unique separators and create arrays in JS

    c) Make individual requests using ajax for each bit of data I need, such as using ajax to:
    1) ask for how many units are for sale
    2) use the returned number to loop a function that requests the next units name, price, and description
    3) move onto the next needed data


    some pointers or links to tuts / books would be greatly appreciated, thank you!
     
    crath, Jul 22, 2009 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    set the variables in javascript (Ajax) form
    pass them as a string to the URL .. and request the .php handler file.

    echo the results in the .php file, and have that become the data sent back to be displayed in the <div> Data </div>

    Make sure to name your <div> and make sure that all variables named in thr URL string are present, or the request will fail.

    To make Ajax sound easier, and not as complex as you said..
    Ajax allows some javascript code to access a php (handler) file WITHOUT having to refresh the page.
    But, assume that even though the whole page is not being refreshed a small section of it IS .
    That area of thepage that is being refreshed is the area designated in the <div> </div> box

    BTW: you can turn off the borders etc ..and make divs transparent white space etc.
    Ajax aint as hard as it seems at first glance. Its basically an already coded 'engine' perse. .. in that script you will find or have already.
     
    ezprint2008, Jul 22, 2009 IP
  3. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #3
    That had nothing to do with my question, what so ever :p That was a simple, not so efficent description of ajax as a whole :\

    After a bit more time searching, I have found JSON , but would still like to hear what other developers use :)
     
    crath, Jul 22, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I just simply use JQuery's implmentation of it, for example a very simple $.get() function I could not only pass data to a php script, but also process it's return just as easily.

    
    $.get("test.cgi", { name: "John", time: "2pm" },
      function(data){
        alert("Data Loaded: " + data);
      });
    
    Code (markup):
    as an example on Jquery site... that data could be a serialized JSON value of which php can parse just as easily. Jquery itself can serialize a form and other data, so you don't really have to go thru so much trouble trying to form your existing html of data into xml and so forth. And likewise you can either use json libraries in php or the very simple serialize and unserialize commands. In my opinion though the type of data is dictated by its usage, its a little excessive to use Json, when the data you need to pass is only two strings 99% of the time.
     
    kblessinggr, Jul 22, 2009 IP
  5. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    I would go with JSON or creating a dynamic XML file via PHP either dependent on the query string eg. blah.com?section=whatever and then the contents of the JSON and XML are dependent on whatever 'section' is set to.

    Use JS (either JQuery or another library) to read the XML/JSON data and create arrays/variables out of it.

    I would limit the amount of AJAX requests to the page as possible, less traffic, saving bandwidth and it'll give your application higher performance.

    Some links;
    JQuery getJSON
    PHP JSON Library Functions

    Regards,

    Steve
     
    Steve136, Jul 23, 2009 IP
  6. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #6
    I am building content management systems and using ajax to change simple values in a database, and return arrays.

    I doubt I'll start using libraries, but I'll build a JSON => array function and go from there, thanks!
     
    crath, Jul 24, 2009 IP
  7. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    JSON works great for short and simple data transmission. And even cooler, JSON does not get limited in cross-domain. Works great from one domain to another (actually that's the major reason for using JSON)

    However, I would not suggest it for long posts or articles (limit in length), or multiple times of data transmission (since it will use call-back function and make the DOM looks messy) :)). And If im correct, JSON data will not be xml format.
     
    zandigo, Jul 24, 2009 IP
  8. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The other server would need PJSON support for cross-domain communication.
     
    kblessinggr, Jul 24, 2009 IP
  9. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    Sure. But that's not my point. My point is when we have one domain, with server is set up to return JSON request. The other side script, javascript in html, can be placed at any domain. By that, what I mean is we can place the client part of web application at any server we want, but not limited to where the data come from. This is different from AJAX.
     
    zandigo, Jul 24, 2009 IP