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 开发者_C百科an example for this purpose.
You can use json_encode($userdata)
to json encode data in the php file. From the client side you can use jQuery $.parseJSON
function to parse json value. It will return a js object corresponding to the user record.
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
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.
精彩评论