Display JSON Data with jQuery and Ajax
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

28 comments:

  1. To create JSON easily you have to do this:

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

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

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

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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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;
    ? >

    ReplyDelete
  7. 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

    ReplyDelete
  8. i am a begginner.

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

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

    pls help

    ReplyDelete
  10. Madhusudhan G RevankarNovember 15, 2011 at 7:59 AM

    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.

    ReplyDelete
  11. 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?

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

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

    ReplyDelete
  14. 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???

    ReplyDelete
  15. 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????

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

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

    ReplyDelete
  18. Hi Shrinivas,

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

    Thanks,
    Chetan

    ReplyDelete
  19. 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

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

    ReplyDelete
  21. 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

    ReplyDelete
  22. 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?

    ReplyDelete
  23. how to display JSON response in HTML in PHP?

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

    ReplyDelete