0

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 type Commented Apr 28, 2011 at 6:10
  • I found some fantastic tutorials when I googled "json php tutorial" :) Commented Apr 28, 2011 at 6:13
  • and are you going to share a linke @christian, or ... Commented Apr 28, 2011 at 6:26

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.