i want go get some data from server using ajax. i pass an id to server, from that id userrecords are accessed from database in the form of array . now i want to return that array and access values of array using json. please, give me an example for this purpose.
3
-
no the data will comeback as json (text) then you convert it using eval, or jquery has nice support for ajax and json data typeIbu– Ibu2011-04-28 06:10:28 +00:00Commented Apr 28, 2011 at 6:10
-
I found some fantastic tutorials when I googled "json php tutorial" :)Christian– Christian2011-04-28 06:13:12 +00:00Commented Apr 28, 2011 at 6:13
-
and are you going to share a linke @christian, or ...Ibu– Ibu2011-04-28 06:26:22 +00:00Commented Apr 28, 2011 at 6:26
Add a comment
|
3 Answers
You can do something like these with jQuery:
$.ajax({
url: "page.php",
type: "POST",
data: ({id : some_id}),
dataType: "json",
success: function(data){
alert(data.property);
}
}
)
data parameter on the callback function contains the json that your php page return.
In your php file do something like this:
echo json_encode($var);
$var must be an array or StdClass
Comments
Follow this example. Provides a good look at the jQuery (are you using this?) code needed to parse JSON.
http://www.adeepersilence.be/archive/jquerys-getjson-with-php
It runs through a basic example and provides the functionality required to send GET parameters to the PHP script so that you can pull data based on them.
Hope that helps.