<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Made Simple &#187; JavaScript</title>
	<atom:link href="http://www.phpmadesimple.info/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmadesimple.info</link>
	<description>To support simplicity, possiibility and cost effective of PHP</description>
	<lastBuildDate>Tue, 03 Aug 2010 14:13:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Prototype and Scriptaculous tree</title>
		<link>http://www.phpmadesimple.info/2008/08/18/prototype-and-scriptaculous-tree/</link>
		<comments>http://www.phpmadesimple.info/2008/08/18/prototype-and-scriptaculous-tree/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 03:07:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[treeview]]></category>

		<guid isPermaLink="false">http://artemis.com.vn/blogvui/index.php/2008/08/18/prototype-and-scriptaculous-tree/</guid>
		<description><![CDATA[After a week trying to embrace SilverStripe JS treeview, finally I had my friend send me a treeview written on Prototype and Scriptaculous, the TafelTree. There are a dozen of treeview control written in many different JS framework like ExtJS, jQuery, etc. but it takes me long time to find out an Protopye one. Good [...]]]></description>
			<content:encoded><![CDATA[<p>After a week trying to embrace SilverStripe JS treeview, finally I had my friend send me a treeview written on Prototype and Scriptaculous, the <a href="http://tafel.developpez.com/site/lang/en/install.php" target="_blank">TafelTree</a>. There are a dozen of treeview control written in many different JS framework like ExtJS, jQuery, etc. but it takes me long time to find out an Protopye one. Good news for Prototype lovers. Anyway, when your project is stick with Protype and you don&#8217;t want to add jQuery just for a tree then this is a good news.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmadesimple.info/2008/08/18/prototype-and-scriptaculous-tree/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object Oriented Programming with JavaScript &#8211; Before Prototype, jQuery or ExtJS</title>
		<link>http://www.phpmadesimple.info/2008/04/27/object-oriented-programming-with-javascript-before-prototype-jquery-or-extjs/</link>
		<comments>http://www.phpmadesimple.info/2008/04/27/object-oriented-programming-with-javascript-before-prototype-jquery-or-extjs/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 10:13:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript oop]]></category>

		<guid isPermaLink="false">http://artemis.com.vn/blogvui/index.php/2008/04/27/object-oriented-programming-with-javascript-before-prototype-jquery-or-extjs/</guid>
		<description><![CDATA[Speaking a language fluently is the key to leverage that language&#8217;s power. JavaScript has a long time being considered a difficult language to learn and use, making the Web user interface less interactive and sometimes, so boring. But in recent years, AJAX term has become so popular and many JS frameworks born to facilitate the [...]]]></description>
			<content:encoded><![CDATA[<p>Speaking a language fluently is the key to leverage that language&#8217;s power. JavaScript has a long time being considered a difficult language to learn and use, making the Web user interface less interactive and sometimes, so boring. But in recent years, AJAX term has become so popular and many JS frameworks born to facilitate the power of JavaScript. Well-known JS frameworks which are the prominent on the web are Prototype, jQuery, Mootool, ExtJs and YUI.<span id="more-30"></span></p>
<p>Most of beginners will found difficulties getting start with these frameworks. Main reason is traditional JS programmers do not use JS heavily in their web applications or use it in an unorganized way, without any best practice pattern. While OOP is not an easy programming method, using it in JS is also headache because of JS syntax. Many programmers use JS in procedural programming style by creating functions and hooking them up with events.</p>
<p>When the term Web 2.0 become a standard, JS is also become a key programming skill that a web programmer need to know. The procedural JS programming should be forget and with the coming of Prototype, jQuery or other JS framework the age of JS programming began. In this post, I want to cover the basic of JS language to build a solid based for JS programmers from where they can easily taking JS frameworks into use. JavaScript can do a lot of clever things that Java and C# can&#8217;t. Some of these can help you to write better code, and some can only help you to shoot yourself in the foot more accurately! It&#8217;s worth knowing about both, either to make use of the techniques or to avoid doing them unwittingly.</p>
<h1>Objects in JavaScript</h1>
<p>JavaScript doesn&#8217;t require the use of objects or even functions. It is possible to write a JavaScript program as a single stream of text that is executed directly as it is read by the interpreter. As a program gets bigger, though, functions and objects become a tremendously useful way of organizing your code, and we recommend you use both.</p>
<p>The simplest way to create a new JavaScript object is to invoke the built-in constructor for the Object class:</p>
<p>var myObject=new Object();</p>
<p>Let&#8217;s add a property to our simple object:</p>
<p>myObject.shoeSize=&#8221;12&#8243;;</p>
<p>In a structured OO language, we would need to define a class that declared a property shoeSize or else suffer a compiler error. Not so with JavaScript. In fact, just to emphasize the array-like nature, we can also reference properties using array syntax:</p>
<p>myObject['shoeSize']=&#8221;12&#8243;;</p>
<p>This notation is clumsy for ordinary use but has the advantage that the array index is a JavaScript expression, offering a form of runtime reflection.</p>
<p>We can also add a new function to our object dynamically:</p>
<pre class="brush: javascript">

myObject.speakYourShoeSize=function(){

alert(&quot;shoe size : &quot;+this.shoeSize);

}
</pre>
<p>Or borrow a predefined function:</p>
<pre class="brush: javascript">function sayHello(){

alert(&#039;hello, my shoeSize is &#039;+this.shoeSize);

}

...

myObject.sayHello=sayHello;
</pre>
<p>Note that in assigning the predefined function, we omit the parentheses. If we were to write</p>
<p>myObject.sayHello=sayHello();</p>
<p>then we would execute the sayHello function and assign the return value, in this case null, to the sayHello property of myObject.</p>
<p>We can attach objects to other objects in order to build up complex data models and so on:</p>
<pre class="brush: javascript">

var myLibrary=new Object();

myLibrary.books=new Array();

myLibrary.books[0]=new Object();

myLibrary.books[0].title=&quot;Turnip Cultivation through the Ages&quot;;

myLibrary.books[0].authors=new Array();

var jim=new Object();

jim.name=&quot;Jim Brown&quot;;

jim.age=9;

myLibrary.books[0].authors[0]=jim;
</pre>
<p>This can quickly become tedious (often the case where turnips are involved, I&#8217;m afraid), and JavaScript offers a compact notation that we can use to assemble object graphs more quickly, known as JSON. <a href="http://artemis.com.vn/blogvui/index.php/2008/04/27/more-about-json/" target="_blank">Have a look at it now</a>.</p>
<h1>Making classes</h1>
<p>JavaScript has a concept of objects and classes but no built-in concept of inheritance. In fact, every JavaScript object is really an instance of the same base class, a class that is capable of binding member fields and functions to itself at runtime.</p>
<p>In JavaScript, then, we can write something that looks similar to the Java declaration</p>
<p>var myObj=new MyObject();</p>
<p>but we do not define a class MyObject, but rather a function with the same name. Here is a simple constructor:</p>
<pre class="brush: javascript">

function MyObject(name,size){

this.name=name;

this.size=size;

}
</pre>
<p>We can subsequently invoke it as follows:</p>
<pre class="brush: javascript">var myObj=new MyObject(&quot;tiddles&quot;,&quot;7.5 meters&quot;);

alert(&quot;size of &quot;+myObj.name+&quot; is &quot;+myObj.size);
</pre>
<p>One common idiom is to declare the function inside the constructor:</p>
<pre class="brush: javascript">function MyObject(name,size){

this.name=name;

this.size=size;

this.tellSize=function(){

alert(&quot;size of &quot;+this.name+&quot; is &quot;+this.size);

}

}

var myObj=new Object(&quot;tiddles&quot;,&quot;7.5 meters&quot;);

myObj.tellSize();
</pre>
<p>This works, but is less than ideal in two respects. First, for every instance of MyObject that we create, we create a new function. As responsible Ajax programmers, memory leaks are never far from our minds, and if we plan on creating many such objects, we should certainly avoid this idiom.</p>
<p>A prototype is a property of JavaScript objects, for which no real equivalent exists in OO languages. Functions and properties can be associated with a constructor&#8217;s prototype. The prototype and new keyword will then work together, and, when a function is invoked by new, all properties and methods of the prototype for the function are attached to the resulting object. That sounds a bit strange, but it&#8217;s simple enough in action:</p>
<pre class="brush: javascript">function MyObject(name,size){

this.name=name;

this.size=size;

}

MyObject.prototype.tellSize=function(){

alert(&quot;size of &quot;+this.name+&quot; is &quot;+this.size);

}

var myObj=new MyObject(&quot;tiddles&quot;,&quot;7.5 meters&quot;);

myObj.tellSize();
</pre>
<p>First, we declare the constructor as before, and then we add functions to the prototype. When we create an instance of the object, the function is attached. The keyword this resolves to the object instance at runtime, and all is well.</p>
<p>Note the ordering of events here. We can refer to the prototype only after the constructor function is declared, and objects will inherit from the prototype only what has already been added to it before the constructor is invoked. The prototype can be altered between invocations to the constructor, and we can attach anything to the prototype, not just a function:</p>
<pre class="brush: javascript">MyObject.prototype.color=&quot;red&quot;;

var obj1=new MyObject();

MyObject.prototype.color=&quot;blue&quot;;

MyObject.prototype.soundEffect=&quot;boOOOoing!!&quot;;

var obj2=new MyObject();
</pre>
<p>obj1 will be red, with no sound effect, and obj2 will be blue with an annoyingly cheerful sound effect! There is generally little value in altering prototypes on the fly in this way. It&#8217;s useful to know that such things can happen, but using the prototype to define class-like behavior for JavaScript objects is the safe and sure route.</p>
<p><a href="http://phrogz.net/JS/Classes/OOPinJS.html" target="_blank">More about declaring Class with prototype</a></p>
<p>Interestingly, the prototype of certain built-in classes (that is, those implemented by the browser and exposed through JavaScript, also known as host objects) can be extended, too. Let&#8217;s have a look at how that works now.</p>
<h1>Extending built-in classes</h1>
<p>Within the web browser, DOM nodes cannot be extended in the Internet Explorer browser, but other core classes work across all major browsers. Let&#8217;s take the Array class as an example and define a few useful helper functions:</p>
<pre class="brush: javascript">Array.prototype.indexOf=function(obj){

var result=-1;

for (var i=0;i&lt; this.length;i++){

if (this[i]==obj){

result=i;

break;

}

}

return result;

}
</pre>
<p>This provides an extra function to the Array object that returns the numerical index of an object in a given array, or -1 if the array doesn&#8217;t contain the object. We can build on this further, writing a convenience method to check whether an array contains an object:</p>
<pre class="brush: javascript">Array.prototype.contains=function(obj){

return (this.indexOf(obj)&gt;=0);

}
</pre>
<p>and then add another function for appending new members after optionally checking for duplicates:</p>
<pre class="brush: javascript">Array.prototype.append=function(obj,nodup){

if (!(nodup &amp;&amp; this.contains(obj))){

this[this.length]=obj;

}

}
</pre>
<p>Any Array objects created after the declaration of these functions, whether by the new operator or as part of a JSON expression, will be able to use these functions:</p>
<pre class="brush: javascript">var numbers=[1,2,3,4,5];

var got8=numbers.contains(8);

numbers.append(&quot;cheese&quot;,true);
</pre>
<h1>Reflecting on JavaScript objects</h1>
<p>In the normal course of writing code, the programmer has a clear understanding of how the objects he is dealing with are composed, that is, what their properties and methods do. In some cases, though, we need to be able to deal with completely unknown objects and discover the nature of their properties and methods before dealing with them. For example, if we are writing a logging or debugging system, we may be required to handle arbitrary objects dumped on us from the outside world. This discovery process is known as reflection, and it should be familiar to most Java and .NET programmers.</p>
<p>If we want to find out whether a JavaScript object supports a certain property or method, we can simply test for it:</p>
<pre class="brush: javascript">if (MyObject.someProperty){

...

}
</pre>
<p>This will fail, however, if MyObject.someProperty has been assigned the boolean value false, or a numerical 0, or the special value null. A more rigorous test would be to write</p>
<p>if (typeof(MyObject.someProperty) != &#8220;undefined&#8221;){</p>
<p>If we are concerned about the type of the property, we can also use the instanceof operator. This recognizes a few basic built-in types:</p>
<pre class="brush: javascript">if (myObj instanceof Array){

...

}else if (myObj instanceof Object){

...

}
</pre>
<p>as well as any class definitions that we define ourselves through constructors:</p>
<pre class="brush: javascript">if (myObj instanceof MyObject){

...

}
</pre>
<p>If you do like using instanceof to test for custom classes, be aware of a couple of &#8220;gotchas.&#8221; First, JSON doesn&#8217;t support it-anything created with JSON is either a JavaScript Object or an Array. Second, built-in objects do support inheritance among themselves. Function and Array, for example, both inherit from Object, so the order of testing matters. If we write</p>
<pre class="brush: javascript">function testType(myObj){

if (myObj instanceof Array){

alert(&quot;it&#039;s an array&quot;);

}else if (myObj instanceof Object){

alert(&quot;it&#039;s an object&quot;);

}

}

testType([1,2,3,4]);
</pre>
<p>and pass an Array through the code, we will be told-correctly-that we have an Array. If, on the other hand, we write</p>
<pre class="brush: javascript">function testType(myObj){

if (myObj instanceof Object){

alert(&quot;it&#039;s an object&quot;);

}else if (myObj instanceof Array){

alert(&quot;it&#039;s an array&quot;);

}

}

testType([1,2,3,4]);
</pre>
<p>then we will be told that we have an Object, which is also technically correct but probably not what we intended.</p>
<p>Finally, there are times when we may want to exhaustively discover all of an object&#8217;s properties and functions. We can do this using the simple for loop:</p>
<pre class="brush: javascript">function MyObject(){

this.color=&#039;red&#039;;

this.flavor=&#039;strawberry&#039;;

this.azimuth=&#039;45 degrees&#039;;

this.favoriteDog=&#039;collie&#039;;

}

var myObj=new MyObject();

var debug=&quot;discovering...\n&quot;;

for (var i in myObj){

debug+=i+&quot; -&gt; &quot;+myObj[i]+&quot;\n&quot;;

}

alert(debug);
</pre>
<p>This loop will execute four times, returning all the values set in the constructor. The for loop syntax works on built-in objects, too-the simple debug loop above produces very big alert boxes when pointed at DOM nodes!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmadesimple.info/2008/04/27/object-oriented-programming-with-javascript-before-prototype-jquery-or-extjs/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>More about JSON</title>
		<link>http://www.phpmadesimple.info/2008/04/27/more-about-json/</link>
		<comments>http://www.phpmadesimple.info/2008/04/27/more-about-json/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 09:06:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[oop with json]]></category>

		<guid isPermaLink="false">http://artemis.com.vn/blogvui/index.php/2008/04/27/more-about-json/</guid>
		<description><![CDATA[After the first post, JSON Quick start, you know what JSON is and how useful it&#8217;s in AJAX applications. This is more about JSON with more examples and advanced usages.
Using JSON
The JavaScript Object Notation (JSON) is a core feature of the language. It provides a concise mechanism for creating arrays and object graphs. In order [...]]]></description>
			<content:encoded><![CDATA[<p>After the first post, JSON Quick start, you know what JSON is and how useful it&#8217;s in AJAX applications. This is more about JSON with more examples and advanced usages.<span id="more-31"></span></p>
<h1>Using JSON</h1>
<p>The JavaScript Object Notation (JSON) is a core feature of the language. It provides a concise mechanism for creating arrays and object graphs. In order to understand JSON, we need to know how JavaScript arrays work, so let&#8217;s cover the basics of them first.</p>
<p>JavaScript has a built-in Array class that can be instantiated using the new keyword:</p>
<pre class="brush: javascript">

myLibrary.books=new Array();
</pre>
<p>Arrays can have values assigned to them by number, much like a conventional C or Java array:</p>
<pre class="brush: javascript">

myLibrary.books[4]=somePredefinedBook;
</pre>
<p>Or they can be associated with a key value, like a Java Map or Python Dictionary, or, indeed, any JavaScript Object:</p>
<pre class="brush: javascript">
myLibrary.books[&quot;BestSeller&quot;]=somePredefinedBook;
</pre>
<p>This syntax is good for fine-tuning, but building a large array or object in the first place can be tedious. The shorthand for creating a numerically indexed array is to use square braces, with the entries being written as a comma-separated list of values, thus:</p>
<pre class="brush: javascript">
myLibrary.books=[predefinedBook1,predefinedBook2,predefinedBook3];
</pre>
<p>And to build a JavaScript Object, we use curly braces, with each value written as a key:value pair:</p>
<pre class="brush: javascript">
myLibrary.books={

bestSeller : predefinedBook1,

cookbook : predefinedBook2,

spaceFiller : predefinedBook3

};
</pre>
<p>In both notations, extra white space is ignored, allowing us to pretty-print for clarity. Keys can also have spaces in them, and can be quoted in the JSON notation, for example:</p>
<p>&#8220;Best Seller&#8221; : predefinedBook1,</p>
<p>We can nest JSON notations to create one-line definitions of complex object hierarchies (albeit rather a long line):</p>
<pre class="brush: javascript">
var myLibrary={

location : &quot;my house&quot;,

keywords : [ &quot;root vegetables&quot;, &quot;turnip&quot;, &quot;tedium&quot; ],

books: [

{

title : &quot;Turnip Cultivation through the Ages&quot;,

authors : [

{ name: &quot;Jim Brown&quot;, age: 9 },

{ name: &quot;Dick Turnip&quot;, age: 312 }

],

publicationDate : &quot;long ago&quot;

},

{

title : &quot;Turnip Cultivation through the Ages, vol. 2&quot;,

authors : [

{ name: &quot;Jim Brown&quot;, age: 35 }

],

publicationDate : new Date(1605,11,05)

}

]

};
</pre>
<p>Sharp-eyed readers will have noted that we populated the publication date for the second book using a JavaScript Date object. In assigning the value we can use any JavaScript code, in fact, even a function that we defined ourselves:</p>
<pre class="brush: javascript">
function gunpowderPlot(){

return new Date(1605,11,05);

}

var volNum=2;

var turnipVol2={

title : &quot;Turnip Cultivation through the Ages, vol. &quot;+volNum,

authors : [

{ name: &quot;Jim Brown&quot;, age: 35 }

],

publicationDate : gunpowderPlot()

}
</pre>
<p>We can also define member functions for our JSON-invoked objects, which can be invoked later by the object:</p>
<pre class="brush: javascript">
var turnipVol2={

title : &quot;Turnip Cultivation through the Ages, vol. &quot;+volNum,

authors : [

{ name: &quot;Jim Brown&quot;, age: 35 }

],

publicationDate : gunpowderPlot()

},

summarize:function(len){

if (!len){ len=7; }

var summary=this.title+&quot; by &quot; + this.authors[0].name +&quot; and his cronies is very boring. Z&quot;;

for (var i=0;i&lt; len;i++){

summary+=&quot;z&quot;;

}

alert(summary);

}

};

...

turnipVol2.summarize(6);
</pre>
<p>The summarize() function has all the features of a standard JavaScript function, such as parameters and a context object identified by the keyword this. Indeed, once the object is created, it is just another JavaScript object, and we can mix and match the JavaScript and JSON notations as we please. We can use JavaScript to fine-tune an object declared in JSON:</p>
<pre class="brush: javascript">
var numbers={ one:1, two:2, three:3 };

numbers.five=5;
</pre>
<p>We initially define an object using JSON syntax and then add to it using plain JavaScript. Equally, we can extend our JavaScript-created objects using JSON:</p>
<pre class="brush: javascript">
var cookbook=new Object();

cookbook.pageCount=321;

cookbook.author={

firstName: &quot;Harry&quot;,

secondName: &quot;Christmas&quot;,

birthdate: new Date(1900,2,29),

interests: [&quot;cheese&quot;,&quot;whistling&quot;,&quot;history of lighthouse keeping&quot;]

};
</pre>
<p>With the built-in JavaScript Object and Array classes and the JSON notation, we can build object hierarchies as complicated as we like, and we could get by with nothing else. JavaScript also offers a means for creating objects that provides a comforting resemblance to class definitions for OO programmers, so let&#8217;s look at this next and see what it can offer us.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmadesimple.info/2008/04/27/more-about-json/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSON quick start</title>
		<link>http://www.phpmadesimple.info/2008/01/19/json-quick-start/</link>
		<comments>http://www.phpmadesimple.info/2008/01/19/json-quick-start/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 05:13:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax with json]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[level 1]]></category>
		<category><![CDATA[object notation]]></category>

		<guid isPermaLink="false">http://artemis.com.vn/blogvui/?p=23</guid>
		<description><![CDATA[What does it stand for?
JavaScript Object Notation.
According to Douglas Corckford’s website, JSON is a lightweight data-interchange format. It is easy for humans to read and write… JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, [...]]]></description>
			<content:encoded><![CDATA[<h3>What does it stand for?</h3>
<p>JavaScript Object Notation.</p>
<p>According to Douglas Corckford’s website, JSON is a lightweight data-interchange format. It is easy for humans to read and write… JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.&#8221;</p>
<p>Object Notation can be expressed in the following format:</p>
<pre class="brush: javascript">
var obj = {
// string
a : &#039;sampleValue&#039;,
// array
b : [obj1,obj2,&#039;three&#039;,4,obj5],
// function
c : function() {
var bang = this.a;
}
}
</pre>
<h3>JSON is built on two structures:</h3>
<ul>
<li>A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.</li>
<li>An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.</li>
</ul>
<h3>Reasons for JSON</h3>
<ul>
<li>JSON is easy. No really. It’s so easy, it’ll make you sick.</li>
<li>If you’re familiar with writing classes in PHP, then you’ll most definitely be comfortable with writing JavaScript in Object Notation</li>
<li>JSON is nothing more than name : value pairs assigned within an object.</li>
<li>JSON is easy to understand because if written well, it’s a self-documenting structure.</li>
<li>JSON is fast!</li>
<li>JSON organizes the ugly mess of procedural programming. Imagine having more than one init function.</li>
<li>You can impress your friends with JSON because it’s pretty looking</li>
<li>Your co-workers will love you for writing in JSON because it will most likely not conflict with their scripts that are being called within the same web documents.</li>
</ul>
<h3>JSON is like XML because:</h3>
<ol>
<li>They are both &#8217;self-describing&#8217; meaning that values are named, and thus &#8216;human readable&#8217;</li>
<li>Both are hierarchical. (i.e. You can have values within values.)</li>
<li>Both can be parsed and used by lots of programming languages</li>
<li>Both can be passed around using AJAX (i.e. httpWebRequest)</li>
</ol>
<h3>JSON is UNlike XML because:</h3>
<ol>
<li>XML uses angle brackets, with a tag name at the start and end of an element: JSON uses squiggly brackets with the name only at the beginning of the element.</li>
<li>JSON is less verbose so it&#8217;s definitely quicker for humans to write, and probably quicker for us to read.</li>
<li>JSON can be parsed trivially using the eval() procedure in JavaScript</li>
<li>JSON includes arrays {where each element doesn&#8217;t have a name of its own}</li>
<li>In XML you can use any name you want for an element, in JSON you can&#8217;t use reserved words from javascript</li>
</ol>
<h3>But Why? What&#8217;s good about it?</h3>
<p>When you&#8217;re writing ajax stuff, if you use JSON, then you avoid hand-writing xml. This is quicker. Also, with ajax services support JSON output, you can bypass difficulties of cross-domain ajax calls.</p>
<p>Again, when you&#8217;re writing ajax stuff, which looks easier? the XML approach or the JSON approach:<br />
The XML approach:</p>
<ol>
<li>bring back an XML document</li>
<li>loop through it, extracting values from it</li>
<li>do something with those values, etc,</li>
</ol>
<p>versus the JSON approach:</p>
<ol>
<li>bring back a JSON string.</li>
<li>&#8216;eval&#8217; the JSON</li>
</ol>
<h3>Example of using JSON in AJAX to transmit data</h3>
<p>The example is about to submit a form to a PHP page using AJAX. To keep the example simple, no data is sent (through POST) to the PHP page. Rather, data is just retrieved from the PHP page. The JSMX javascript library is very lightweight (code-wise) and sends and retrieves our data for us. The PHP page (that we make our requests to) simply prints out the data it wants to send back in a ready-to-use javascript object format.</p>
<p>&lt;img src=&#8221;http://simpletutorials.com/tutorials/javascript/simple_ajax/ajax_diagram.jpg&#8221; alt=&#8221;" /&gt;</p>
<pre class="brush: php">
&lt; ?php

require_once &quot;json/JSON.php&quot;;
$json = new Services_JSON();

//convert php object to json

$value = array(&#039;first&#039; =&gt; &#039;Steven&#039;, &#039;last&#039; =&gt; &#039;Spielberg&#039;, &#039;address&#039; =&gt; &#039;1234 Unlisted Drive&#039;);

$output = $json-&gt;encode($value);

print($output);

?&gt;
</pre>
<p>The JSON-PHP module can convert a PHP array of key value pairs to this JSON object.</p>
<pre class="brush: javascript">
{&quot;first&quot;:&quot;Steven&quot;,&quot;last&quot;:&quot;Spielberg&quot;,&quot;address&quot;:&quot;1234 Unlisted Drive&quot;}
</pre>
<p>HTML page</p>
<pre class="brush: html">
&lt;html&gt;
&lt;head&gt;

&lt;script src=&quot;ajax.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;

/**Ajax Request (Submits the form below through AJAX
*               and then calls the ajax_response function)
*/
function ajax_request() {
var submitTo = &#039;ajax_request.php&#039;;
//location.href = submitTo; //uncomment if you need for debugging

http(&#039;POST&#039;, submitTo, ajax_response, document.form1);
}

/**Ajax Response (Called when ajax data has been retrieved)
*
* @param   object  data   Javascript (JSON) data object received
*                         through ajax call
*/
function ajax_response(data) {
for(var key in data) {
document.form1[key].value = data[key];
}
}

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;input type=&quot;button&quot; onclick=&quot;ajax_request()&quot; value=&quot;Do AJAX&quot;/&gt;&lt;br /&gt;&lt;br /&gt;

&lt;form name=&quot;form1&quot;&gt;
First &lt;input type=&quot;text&quot; name=&quot;first&quot;/&gt;&lt;br /&gt;
Last  &lt;input type=&quot;text&quot; name=&quot;last&quot;/&gt;&lt;br /&gt;
Address &lt;input type=&quot;text&quot; name=&quot;address&quot;/&gt;&lt;br /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This tutorial demonstrates how to perform AJAX functionality simply and effectively, using the <a href="http://www.lalabird.com/" target="_blank">AJAX JSMX library</a>, coupled with the <a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198" target="_blank">JSON-PHP  library</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmadesimple.info/2008/01/19/json-quick-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
