SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax. What is the easiest way to recieve data from PHP?

    Hello guys,

    I've read view articles about how to dynamically recieve data from server with Ajax, but they are too complicated for me. I wonder what technique do you use? Which one is the easiest one?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out the JSON library @ http://json.org

  3. #3
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've used different ways of fetching data from a PHP file.
    I started out using just strings, in which name-value pairs are separated by a certain character, like this;

    Code:
    foo=70::--::bar=90::--::baz=19
    
    // split string on "::--::"
    This is very light-weight and works fine in most cases, but it's much easier to print some XML with PHP. The biggest advantage is that you can use the build-in DOM methods, such as getElementsByTagName().

  4. #4
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    check out the JSON library @ http://json.org
    Thanks Jim, I already use some JSON one my site (with 3rd party scriprs). But I'd really will be happy to see some eample to see how does it work with JSON. If you don't have a link to any exaple, I'll ve glad to listen about idea where can I find it.

    Quote Originally Posted by Ize View Post
    This is very light-weight and works fine in most cases, but it's much easier to print some XML with PHP. The biggest advantage is that you can use the build-in DOM methods, such as getElementsByTagName().
    Can you show me some example please (even some small one), so I can inderstand how it works?

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are several PHP JSON libraries listed on that page with a comparison of the pros/cons of each.

    On the javascript side, the library gives you 2 main functions:

    foo.toJSONString() - returns a serialized string that holds the data contained in foo ready to send via ajax or whatever to php

    string.parseJSON() - takes a serialized string and returns a variable/data structure

  6. #6
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    There are several PHP JSON libraries listed on that page with a comparison of the pros/cons of each.

    On the javascript side, the library gives you 2 main functions:

    foo.toJSONString() - returns a serialized string that holds the data contained in foo ready to send via ajax or whatever to php

    string.parseJSON() - takes a serialized string and returns a variable/data structure
    Thanks Jim. However I wouldn't call this simple. I wonder, is there any similar method to get data from PHP as this simple way to send data to PHP?

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mentioned Ajax in your first post - this refers to using xmlhttprequest objects. The technique used in your link creates image objects and uses them to send data to the server, but you can only receive data from php to javascript using xmlhttprequest. So I think you'll have to bite the bullet.

  8. #8
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    You mentioned Ajax in your first post - this refers to using xmlhttprequest objects. The technique used in your link creates image objects and uses them to send data to the server, but you can only receive data from php to javascript using xmlhttprequest. So I think you'll have to bite the bullet.
    No problem, I will learn JSON/PHP method. I just wanted to find out simpliest way to receive data. It seems this method is simpliest...

    Thanks for help Jim.

  9. #9
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The simpliest method of getting data from PHP is..

    echo $str;

    Usually all you're doing is fetching some simple strings from PHP, and all you have to do is output it. Which will get picked up by ajax.responseText.

  10. #10
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is true - JSON is a much more complex model used if you need to retrieve arrays, objects, etc from PHP. If you are simply displaying text or HTML retrieved via AJAX from the server you can just write out the text to a div. Here's an example I found at http://www.omnytex.com/articles/xhrstruts/
    Code:
    <html>
    
    <head>
    
    <title>Example 2</title>
    
     
    
    <script>
    
     
    
      var req;
    
      var which;
    
     
    
      function retrieveURL(url) {
    
        if (window.XMLHttpRequest) { // Non-IE browsers
    
          req = new XMLHttpRequest();
    
          req.onreadystatechange = processStateChange;
    
          try {
    
            req.open("GET", url, true);
    
          } catch (e) {
    
            alert(e);
    
          }
    
          req.send(null);
    
        } else if (window.ActiveXObject) { // IE
    
          req = new ActiveXObject("Microsoft.XMLHTTP");
    
          if (req) {
    
            req.onreadystatechange = processStateChange;
    
            req.open("GET", url, true);
    
            req.send();
    
          }
    
        }
    
      }
    
     
    
      function processStateChange() {
    
        if (req.readyState == 4) { // Complete
    
          if (req.status == 200) { // OK response
    
            document.getElementById("theTable").innerHTML = req.responseText;
    
          } else {
    
            alert("Problem: " + req.statusText);
    
          }
    
        }
    
      }
    
     
    
    </script>
    
     
    
    </head>
    
    <body onLoad="retrieveURL('example2RenderTable.do');">
    
     
    
    <h1>Example 2</h1>
    
    Dynamic table.<hr>
    
    <p align="right"><a href="home.do">Return home</a></p><br>
    
    This example shows how a table can be built and displayed on-the-fly by showing
    
    sorting of a table based on clicks on the table headers.
    
    <br><br>
    
     
    
    <span id="theTable"></span>
    
    <br>
    
     
    
    </body>
    
    </html>

  11. #11
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    For one dimensional arrays you can just echo out a | (or whatever) separated string, and with javascript just split it and create the array you need.

  12. #12
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would you say about this tutorial? I've tried their sample on my server, but it didn't work. They have working sample in the end of the tutorial, but it doesn't work also (Firefox, Mac).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •