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?
| SitePoint Sponsor |




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?





check out the JSON library @ http://json.org




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;
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().Code:foo=70::--::bar=90::--::baz=19 // split string on "::--::"




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.
Can you show me some example please (even some small one), so I can inderstand how it works?





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?





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.




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.





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>
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.




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