Getting a PHP json_encode object into jQuery


For the love of Code

Sometimes you might find yourself in a situation where you have to send a bunch of text obtained from a database query, to jQuery or JS script to insert it into the DOM.

One of the ways to do that is to convert the data obtained from a PHP query, into a JSON object, and then inject this into the DOM asynchronously ( using AJAX, of course). This can be done as follows :

First lets talk about the structure of a simple json object :

  1. {

  2. “name”: “Solomon”,

  3. “age” : “67”,

  4. “sex”: “male”

  5. }

Its similar to an associative array with some “key” : “value”  pairs. Both the keys and values are written in quotes. To access these values in Javascript, this json object is assigned to a variable :

  1. var json =  { “name”: “Zara Ali”, “age” : “67”,”sex”: “female”};

  2. console.log(json.name);       //this prints “Zara Ali” on the browser’s console

View original post 577 more words