Social Networks

I was trying to find the best way to display results on my page via an Ajax call using jQuery, do you think the best way is to pass it as JSON or plain text?

Yes you are right. JQuery has an inbuilt json data type for Ajax and converts the data into a object. PHP% also has inbuilt json_encode function which converts an array into json formatted string. Saves a lot of parsing, decoding effort.



JSON (Java Script Object Notation) is a lightweight data passing format and human readable contains java structures. This post 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.


Download Script

download script


View online Demo


XML Structure


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

<title>jQuery and Ajax Demos</title>


JSON Structure 

"title":"9lessons | Programming Blog", 
"title":"jQuery and Ajax Demos Pard - 3", 

Javascript Code
Loading data.js(json file) data using jQuery and Ajax.

<script type="text/javascript" src="
<script type="text/javascript">
$.each(data.posts, function(i,data)
var div_data =
"<div ><a href='"+data.url+"'>"+data.title+"</a></div>";
return false;


Load Click
Load data while clicking just replace javascript code : $(document).ready(function() to$(‘.load’).click(function()

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)

source: 9lession and stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *

Allowed Tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>