Wall Script
Follow Me:
Wednesday, October 14, 2009

Display JSON Data with jQuery and Ajax

JSON (Java Script Object Notation) is a lightweight data passing format and human readable contains java structures. In this post I want to explain Creating JSON file with PHP and Display JSON data using jquery and Ajax. Using JSON we can interchange data between the browsers and the server.

Display JSON Data with jQuey and Ajax

Download Script     Live Demo
XML Structure

<title>9lessons | Programming Blog</title>

<title>jQuery and Ajax Demos</title>


JSON Structure
Take a look at data.js file click here
"title":"9lessons | Programming Blog",
"title":"jQuery and Ajax Demos Pard - 3",

Javascript Code
Loading data.js(json file) data using jQuery and Ajax. Take a look at live preview click here
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
<script type="text/javascript">
$.each(data.posts, function(i,data)
var div_data =
"<div ><a href='"+data.url+"'>"+data.title+"</a></div>";
return false;

<div id="9lessonsLinks"></div>

Load Click
Load data while clicking just replace javascript code : $(document).ready(function() to $('.load').click(function() Live Preview Click Here
return false;
<input type="button" value=" Load " class="load" />
<div id="9lessonsLinks"></div>

Creating JSON file with PHP

Database Table Posts
title VARCHAR(200),
url VARCHAR(200)

Contains simple PHP code.
$sql=mysql_query("select * from Posts limit 20");
echo '{"posts": [';
echo '
echo ']}';

Loading PHP file

If you want to load PHP file replace javascript code $.getJSON("data.js",function(data)

9lessons Best Article

Benjamin said...

To create JSON easily you have to do this:

$result['title'] = $title;
$result['url'] = $url;
$result = json_encode($result);
echo $result;

Faust said...


Anonymous said...

If Call data.js from diferent domain will script work?
$.getJSON("http://domain.com/data.js", function(data)

Anonymous said...

Nice one article will u plz post same one in asp.net rather than php !!

Ripon said...

i have found the PHP part useful but the tailoring ',' made the total process unusable. I had to truncate the last occurrence of comma ',' programmability.

Arifur Rahman said...

you can use json encode for this you can use this as < ?php

$result ="";
$result[$i]['title'] = "title " + $i;
$result[$i]['url'] = "url " + $i ;
$result = json_encode($result);
echo $result;
? >

Majid said...

How when clicking:
"button" Load "

we want to send an id so the file :

can filter the sql statement and returns only data relative to that id ?


Anonymous said...

i am a begginner.

suppose if we want the data to be displayed as
var products=["+data.url+"]; then how?

Anonymous said...

i want to display the data as var=[] from php so how can i proceed

pls help

Anonymous said...

nice tutorial

Madhusudhan G Revankar said...

Hi ,

I am trying to implement display the contents frm database to my webpage using JSON and Jquery,here i would like to implement in java,Can i use it as above or is there any type of changes required here? Please share the code ,it will be very grateful for me.

Thanks in Advance.

Josh said...

I used this tutorial and it works great, however it does not work with jQuery 1.7. Would it be possible to get it working with the current release of jQuery?

agyrágóbogár said...

I also tried it with newer jquery and unfortunately it doesn´t work. An update on this would be greatly appreciated!

Madeinara said...

thanks for tutor.. i'll use this logic ! thanks

mohamedHasni said...

when i choose to load data from a php script i don't have any data. When i execute directly my php script it shows me data. Any help please???

mohamedHasni said...

I can't show data from a php script and i when i test my php script it show correctly my data. ANy help please????

Ronnel said...

I can't show data from a php script using latest jquery.. please help. update the code..

JEEVAN said...

Nice one article will u plz post same one in Jquery rather than php !!

Anonymous said...

very nice

legal helping hand said...

Nice one. Great work.

Anonymous said...

Hi Shrinivas,

You create such a useful demos I have used your demos in various projects.


Anonymous said...

Unable to use multiple name or values.
echo '{"posts": [';

echo '
echo ']}';

How to print them into a table format.

Can you please help me in this regards

Mithun Pattankar said...

demo aspect of this article is really good, makes u understand it quickly...

blog said...


Anonymous said...

Thank you much it was as if the world was crazy or dumb.....i looked for this kind of simple straight forwards tutorial for like 3 days....thank you buddy I am new fun

Anonymous said...

I have a problem with utf8, everything work very well, when I check the mail through www it is ok, but through MS outlook I don't see true utf sign, how can solve this problem?

Anonymous said...

how to display JSON response in HTML in PHP?

Anonymous said...

I've been looking everywhere for something like this. Just a good tutorial on How To. Thank you!!

Post a Comment