<?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>The Incurable Geek &#187; jQuery</title>
	<atom:link href="http://nicolasrosental.com/category/development/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://nicolasrosental.com</link>
	<description>Web Development, Coworking and 42</description>
	<lastBuildDate>Fri, 03 Sep 2010 18:46:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Easy jQuery AJAX For Backend Developers</title>
		<link>http://nicolasrosental.com/2009/03/09/easy-jquery-ajax-for-backend-developers/</link>
		<comments>http://nicolasrosental.com/2009/03/09/easy-jquery-ajax-for-backend-developers/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 18:06:36 +0000</pubDate>
		<dc:creator>nic</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://nicolasrosental.com/?p=112</guid>
		<description><![CDATA[There used to be a time when the backend had most of the power. Us PHP coders and other server-oriented life forms used to say &#8220;JavaScript? No, that&#8217;s for adding lame tricks that don&#8217;t work half the time.&#8221; But that was then, in the past few years AJAX and JavaScript in general exploded. All of [...]]]></description>
			<content:encoded><![CDATA[<p>There used to be a time when the backend had most of the power. Us PHP coders and other server-oriented life forms used to say &#8220;JavaScript? No, that&#8217;s for adding lame tricks that don&#8217;t work half the time.&#8221; But that was then, in the past few years AJAX and JavaScript in general exploded. All of a sudden it isn&#8217;t okay to re-load the page every time we needed to get new data.</p>
<p>I figured it couldn&#8217;t be that hard, but for some reason I was struggling to get AJAX to work like I wanted, and in talking to other developers I found out I wasn&#8217;t alone. For all of you that can relate to my story, here&#8217;s quick tutorial.</p>
<h3>The Setup</h3>
<p>We need a simple HTML page with a block element, with an id of  &#8220;element&#8221;. This page will also have the Jquery/Javascript needed to make the magic happen ( Usually you&#8217;d like to have most of your scripts in a separate file.) Later on we&#8217;ll add more to it, but this is all we need to get started.</p>
<pre>
&lt;!-- import jQuery wherever it is, in this case it's the same dir --&gt;

&lt;script src="jquery.js" type="text/javascript"&gt; &gt;/script&gt; &lt;!-- This is the secret sauce --&lt;
&lt;script type="text/javascript"&gt;
    $(document).ready(function ()
    {
        $('#element').load('output.php');
    });
&lt;/script&gt;
    &gt;div id="element" &gt;/div>
</pre>
<p>Let&#8217;s take a minute to explain the code above. First we need to import jQuery. Then we run our little script which says <em>when the DOM is ready grab the output from output.php and attach it to #element </em>(that is the div with id &#8216;element&#8217;.) The best part of the <strong>load </strong>function is that it expects a piece of HTML to attach to the given selector. It&#8217;s simple, rudimentary, and exactly what backend developers need.</p>
<p>Of course, we also need a very simple PHP script to give us the output we want to bring in via AJAX; I&#8217;ll name the file output.php. Again, we&#8217;ll add some complexity in latter examples.</p>
<pre>
&lt;?php
echo "Hello AJAX";
?&gt;
</pre>
<p>At first sight this example seems as if the &#8220;Hello Ajax!&#8221; message is just part of the page we&#8217;re viewing, so we&#8217;re going to modify the scripts a bit to show that is actually making a call to the script. Alternatively you can use the <a href="http://getfirebug.com/">Firebug</a> console to view the request and script information (highly recommended.)</p>
<h3>Wait for me!</h3>
<p>This time around, instead of making the request as soon as the DOM is ready, we&#8217;re going to do it at the push of a button.</p>
<pre>
&lt;script src="jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;!-- import jQuery wherever it is --&gt; &lt;!-- This is the secret sauce --&gt;
&lt;script type="text/javascript"&gt;
    $(document).ready(function ()
    {
        $("#btn").click(function ()
        {
            $("#element").load('output.php');
        });
    })
&lt;/script&gt;
&lt;div id="element"&gt;&lt;/div&gt;
&lt;input id="btn" type="submit" /&gt;
</pre>
<p>This time around we told the function to wait for the button with id of &#8216;btn&#8217; to be clicked before fetching the output. We do so by using the <strong>click</strong> function of javascript. The same could be accomplished by adding <strong>onClick </strong>to the button in the HTML.</p>
<h3>The Final Act</h3>
<p>In the last example, I&#8217;ll show you how to combine the the power of jQuery and PHP to bring in real HTML to the page. Grab good &#8216;ole output.php and modify it as follows</p>
<pre>

//output.php

echo '&lt;h1&gt;Hello AJAX&lt;/h1&gt;';
echo '&lt;p&gt;This is a paragraph right after the Hello Ajax title&lt;/p&gt;';
</pre>
<p>When you click on the button now, you get properly formatted HTML. Yay AJAX!</p>
<p>Please keep in mind that there are much more advanced ways of doing this (witin and without jQuery) that allow for much more granular control, yet this is an extremely simple but powerful way to get some AJAX rolling.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d112').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d112" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;submitHeadline=Easy+jQuery+AJAX+For+Backend+Developers&amp;submitSummary=" rel="nofollow" title="Add to&nbsp;Buzz"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/buzz.png" title="Add to&nbsp;Buzz" alt="Add to&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;title=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;title=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;title=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;bm_description=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;T=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;title=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;title=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F" rel="nofollow" title="Add to&nbsp;Tip'd"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/tipd.png" title="Add to&nbsp;Tip'd" alt="Add to&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Easy+jQuery+AJAX+For+Backend+Developers+@+http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fnicolasrosental.com%2F2009%2F03%2F09%2Feasy-jquery-ajax-for-backend-developers%2F&amp;t=Easy+jQuery+AJAX+For+Backend+Developers" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://nicolasrosental.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d112').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d112').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://nicolasrosental.com/2009/03/09/easy-jquery-ajax-for-backend-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->