qwerty
qwerty

Reputation: 19

getting array data through ajax

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.

Upvotes: 0

Views: 371

Answers (3)

mdaguerre
mdaguerre

Reputation: 1267

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

Upvotes: 1

Shameer
Shameer

Reputation: 3066

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.

Upvotes: 1

ebrandell
ebrandell

Reputation: 432

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.

Upvotes: 0

Related Questions