9lessons programming blog
Loading Search
9lessons Bring Friends
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
<posts>

<title>9lessons | Programming Blog</title>
<url>http://www.9lessons.info</url>

<title>jQuery and Ajax Demos</title>
<url>jquery-and-ajax-best-demos-part-3.html</url>

</posts>




JSON Structure
Take a look at data.js file click here
{"posts":
[
{
"title":"9lessons | Programming Blog",
"url":"http://www.9lessons.info"
},
{
"title":"jQuery and Ajax Demos Pard - 3",
"url":"jquery-and-ajax-best-demos-part-3.html"
},
]
}

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/
libs/jquery/1.3.0/jquery.min.js
"></script>
<script type="text/javascript">
$(function()
{
$(document).ready(function()
{
$.getJSON("data.js",function(data)
{
$.each(data.posts, function(i,data)
{
var div_data =
"<div ><a href='"+data.url+"'>"+data.title+"</a></div>";
$(div_data).appendTo("#9lessonsLinks");
});
}
);
return false;
});
});
</script>

<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
$(".load").click(function()
{
----
----
return false;
});
<div>
<input type="button" value=" Load " class="load" />
</div>
<div id="9lessonsLinks"></div>

Creating JSON file with PHP

Database Table Posts
CREATE TABLE Posts
{
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(200),
url VARCHAR(200)
}

json_data.php
Contains simple PHP code.
<?php
include('config.php');
$sql=mysql_query("select * from Posts limit 20");
echo '{"posts": [';
while($row=mysql_fetch_array($sql))
{
$title=$row['title'];
$url=$row['url'];
echo '
{
"title":"'.$title.'",
"url":"'.$url.'"
},';
}
echo ']}';
?>

Loading PHP file

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

9lessons Best Article

Comments
{ 24 comments }
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...

yep
http://php.net/manual/en/function.json-encode.php

Anonymous said...

Hi
If Call data.js from diferent domain will script work?
example
$.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 ="";
for($i=0;$i<10;$i++)
{
$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 :
json_data.php

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

Thanks

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.

Thanks,
Chetan

Anonymous said...

hi,
Unable to use multiple name or values.
echo '{"posts": [';
while($row=mysql_fetch_array($myquery))
{
$p_id=$row['p_id'];
$p_name=$row['p_name'];
$p_description=$row['p_description'];
$p_img_url=$row['p_img_url'];
$p_url=$row['p_url'];
$p_pdfurl=$row['p_pdfurl'];

echo '
{
"p_id":"'.$p_id.'",
"p_name":"'.$p_name.'",
"p_description":"'.$p_description.'",
"p_img_url":"'.$p_img_url.'",
"p_url":"'.$p_url.'",
"p_pdfurl":"'.$p_pdfurl.'"
},';
}
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...

kk

Post a Comment