SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
Oct 19, 2007, 23:49 #1
- 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?
-
Oct 21, 2007, 01:17 #2
- 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
-
Oct 21, 2007, 03:07 #3
- 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 "::--::"
-
Oct 21, 2007, 06:08 #4
- Join Date
- Mar 2004
- Posts
- 639
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
-
Oct 21, 2007, 12:48 #5
- 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
-
Oct 23, 2007, 08:44 #6
- Join Date
- Mar 2004
- Posts
- 639
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
-
Oct 23, 2007, 14:18 #7
- 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.
-
Oct 24, 2007, 02:53 #8
- Join Date
- Mar 2004
- Posts
- 639
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Oct 24, 2007, 07:45 #9
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.
-
Oct 24, 2007, 16:39 #10
- 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>
-
Oct 24, 2007, 17:05 #11
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.
-
Oct 25, 2007, 09:11 #12
- 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