<?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>Daniel Gibbons</title>
	<atom:link href="http://danielgibbons.net/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://danielgibbons.net</link>
	<description>How-To</description>
	<lastBuildDate>Sun, 06 Dec 2009 18:26:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamically loading content with Ajax and PHP</title>
		<link>http://danielgibbons.net/?p=6</link>
		<comments>http://danielgibbons.net/?p=6#comments</comments>
		<pubDate>Sun, 06 Dec 2009 16:20:29 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[activexobject]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[dynamically]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie5]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[ie7]]></category>
		<category><![CDATA[intro]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[microsoft.xmlhttp]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[onreadystatechange]]></category>
		<category><![CDATA[opera]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[readystate]]></category>
		<category><![CDATA[safari]]></category>
		<category><![CDATA[to]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[xmlhttprequest]]></category>

		<guid isPermaLink="false">http://danielgibbons.net/?p=6</guid>
		<description><![CDATA[Ajax is a web technique used to dynamically check and load content on the fly without reloading the web browser. It is accomplished using JavaScript and some sort of server side scripting such as PHP. In this tutorial I&#8217;ll show you how to generate a random number quickly without reloading the page using JavaScript and [...]]]></description>
			<content:encoded><![CDATA[<p>Ajax is a web technique used to dynamically check and load content on the fly without reloading the web browser. It is accomplished using JavaScript and some sort of server side scripting such as PHP. In this tutorial I&#8217;ll show you how to generate a random number quickly without reloading the page using JavaScript and PHP. Using this example, it&#8217;s easy to expand upon with a foundation in JavaScript and PHP to produce the result you would like in a large variety of web applications.</p>
<p>In a typical setup, we will need three things:</p>
<ol>
<li>An HTML Page</li>
<li>A JavaScript File</li>
<li>A PHP Page</li>
</ol>
<p>In the HTML page, we will simply need three things. First, we need to include a JavaScript file to include our Ajax in. Another thing would be an action that would call the script. In this case that will be a link that calls the javascript getRand() function. This function will be defined in the JavaScript file we included. The last thing would be somewhere to put our results. In this case, it&#8217;s an empty div tag with the id of displayarea. The HTML file may be called whatever you&#8217;d like, but in this case we will use index.html. Here&#8217;s the complete code for the HTML code in this example:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot;</span>
<span style="color: #00bbdd;">  &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Ajax PHP<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:getRand()&quot;</span>&gt;</span>Show Random Number<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;displayarea&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Because the PHP page is so simple in this case, I&#8217;ll go ahead and explain it before getting into the Ajax part. In this example, all our PHP page will do is display a random number. This is accomplished using PHP&#8217;s rand() function. We will call this file server.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>For the JavaScript file, we will need to include all of the logic for the Ajax request. Basically, we will have to create an XMLHttpRequest object, which will be used to open a link to our dynamic page (PHP) and put the data into our container(the div tag). To add support for old browsers, we can also check to see which kind of object we should create. If we only wanted to support new browsers, we could use this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">xmlHttpRequest <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Because we&#8217;d also like to support Internet Explorer 5 and 6, we will need to write a function to see which kind of object we will take. You can read more about this <a href="http://msdn.microsoft.com/en-us/library/ms537505(VS.85).aspx" target="_blank">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Produce the object for the correct browser</span>
<span style="color: #006600; font-style: italic;">// May be modified to support more older browsers</span>
<span style="color: #003366; font-weight: bold;">function</span> getXmlHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Microsoft Internet Explorer 5 &amp; 6</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// Modern browsers such as Firefox, IE7, Safari, Chrome, and Opera7.6+</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please upgrade your browser to use Ajax&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we need to do something with this object. We made it so when we click the link, it calls the JavaScript function getRand(). With the JavaScript, we can take the XMLHttpRequest object we generated earlier and invoke the open and send functions to load our desired PHP page. The method information for these can be find <a href="http://msdn.microsoft.com/en-us/library/ms536648(VS.85).aspx" target="_blank">here(open)</a> and <a href="http://msdn.microsoft.com/en-us/library/ms763706(VS.85).aspx" target="_blank">here(send)</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Page to open</span>
xmlHttpRequest.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;server.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xmlHttpRequest.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By using true as the 3rd parameter in the open method, it will run a callback to an onreadystatechange method. We can define this method and check to see if the state it&#8217;s sending is when the page has completed loading(4). By doing that, when the open method has finished loading the page, our desired changes in the website will take effect. The send method will send the HTTP request to the server.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This function tells us how far along the page is with loading</span>
xmlHttpRequest.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// When the readystate is 4, the page has been completely loaded</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>xmlHttpRequest.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>
		<span style="color: #006600; font-style: italic;">// What is changed in the document when the page is loaded</span>
		document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;displayarea&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> xmlHttpRequest.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this example, all that happens is the JavaScript finds the displayarea div tag and replaces the html inside of it with the response from our server.php page.</p>
<p>Here&#8217;s the complete code for the getRand() JavaScript method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// When action is called, main logic</span>
<span style="color: #003366; font-weight: bold;">function</span> getRand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Get the XMLHttpRequest Object from our function above that checks</span>
	<span style="color: #006600; font-style: italic;">// browser compatibility and retrieves the correct object</span>
	xmlHttpRequest <span style="color: #339933;">=</span> getXmlHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpRequest <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Page to open</span>
		xmlHttpRequest.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;server.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		xmlHttpRequest.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		<span style="color: #006600; font-style: italic;">// This function tells us how far along the page is with loading</span>
		xmlHttpRequest.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// When the readystate is 4, the page has been completely loaded</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>xmlHttpRequest.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>
				<span style="color: #006600; font-style: italic;">// What is changed in the document when the page is loaded</span>
				document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;displayarea&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> xmlHttpRequest.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This random number generator exmaple of Ajax may be adapted to work with many applications such as a navigation bar or login form. A working example can be found <a href="http://danielgibbons.net/examples/ajax1/">here</a>. The complete source code can be downloaded <a href="http://danielgibbons.net/examples/ajax1/ajax1.zip">here</a>.</p>
<p>More Resources:<br />
<a href="http://www.ibm.com/developerworks/web/library/wa-ajaxintro1.html">http://www.ibm.com/developerworks/web/library/wa-ajaxintro1.html</a><br />
<a href="http://www.w3schools.com/php/php_ajax_intro.asp">http://www.w3schools.com/php/php_ajax_intro.asp</a></p>
]]></content:encoded>
			<wfw:commentRss>http://danielgibbons.net/?feed=rss2&amp;p=6</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ajax the easy way with jQuery</title>
		<link>http://danielgibbons.net/?p=34</link>
		<comments>http://danielgibbons.net/?p=34#comments</comments>
		<pubDate>Sun, 06 Dec 2009 17:03:10 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[intro]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://danielgibbons.net/?p=34</guid>
		<description><![CDATA[To start off, I&#8217;d highly recommend learning about Ajax using XMLHttpRequest objects here. To supplement that tutorial, I&#8217;ll talk about doing Ajax in a much easier, much shorter way using the jQuery JavaScript Library. Using jQuery for Ajax is much quicker, easier, and less time-consuming.
Like a normal Ajax setup, we&#8217;ll need 3 things:

 An HTML [...]]]></description>
			<content:encoded><![CDATA[<p>To start off, I&#8217;d highly recommend learning about Ajax using XMLHttpRequest objects <a href="http://danielgibbons.net/?p=6">here</a>. To supplement that tutorial, I&#8217;ll talk about doing Ajax in a much easier, much shorter way using the <a href="http://jquery.com/">jQuery JavaScript Library</a>. Using jQuery for Ajax is much quicker, easier, and less time-consuming.</p>
<p>Like a normal Ajax setup, we&#8217;ll need 3 things:</p>
<ol>
<li> An HTML Page</li>
<li> A JavaScript File</li>
<li> A server side script (In this case, a PHP page again)</li>
</ol>
<p>We can use the same HTML and PHP pages as in my last example. Those are explained <a href="http://danielgibbons.net/?p=6">here</a>. In short, the HTML page just contains a link to generate a random number. The PHP page contains the code to provide a random number. What the JavaScript file will do is use Ajax to dynamically load the PHP page into the empty div tag we provide when the link is clicked without refreshing the page.<br />
The code for the HTML page is exactly the same as a non-jQuery Ajax example, except that we also have to load the jQuery Library in our head tag.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery-1.3.2.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>This also requires that jQuery is downloaded and put in the same directory as the HTML file (unless the src attribute is changed). jQuery may be downloaded <a href="http://jquery.com">here</a>. Here&#8217;s the full HTML file for this example:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot;</span>
<span style="color: #00bbdd;">  &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Ajax PHP<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery-1.3.2.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:getRand()&quot;</span>&gt;</span>Show Random Number<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;displayarea&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>And the PHP page(server.php):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now, in my previous example, there was a good bit of JavaScript. With jQuery, we only need one line in the method for simple examples like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// When action is called, main logic</span>
<span style="color: #003366; font-weight: bold;">function</span> getRand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#displayarea&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;server.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now I&#8217;ll explain what the does. In jQuery, starting a line with a $ symbol basically tells jQuery that you&#8217;d like to use the library. The first part, $(&#8221;#displayarea&#8221;), selects our displayarea div tag. In jQuery that&#8217;s how you do the JavaScript method getElementById. The <a href="http://docs.jquery.com/Ajax/load#urldatacallback">load method</a> does all the Ajax for us by loading the page and inserting what it retrieves into the div tag we just selected.</p>
<p>By default, such as in the example, the load method will just use GET to load the page. If you supply additional parameters, such as this, a POST will be performed:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// When action is called, main logic</span>
<span style="color: #003366; font-weight: bold;">function</span> getRand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	myvar <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#displayarea&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;server.php&quot;</span><span style="color: #339933;">,</span> attribute<span style="color: #339933;">:</span> myvar <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That attributes may be accessed in the PHP page via $_POST['attribute']. To supply more than one attribute, you can put the whole 2nd parameter in brackets and seperate each attribute by a comma like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// When action is called, main logic</span>
<span style="color: #003366; font-weight: bold;">function</span> getRand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	myvar1 <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
	myvar2 <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;world&quot;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#displayarea&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;server.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> attribute1<span style="color: #339933;">:</span> myvar1<span style="color: #339933;">,</span> attribute2<span style="color: #339933;">:</span> myvar2 <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Furthermore, if you&#8217;d like to specify if you want it to GET or POST, you may just replace load with the method get or post. Since the load method can take 1, 2, or 3 parameters. The 3rd one would be a callback function. This function would be called when the Ajax is complete whether or not it was successful.<br />
Ajax has many more useful applications, and a listing of all the methods may be found here:<br />
<a href="http://docs.jquery.com/Main_Page">http://docs.jquery.com/Main_Page</a><br />
This Ajax random number generator can easily be applied to other applications such as a navigation or login form. You may find a working example <a href="http://danielgibbons.net/examples/ajax2/">here</a> and the source code to each file <a href="http://danielgibbons.net/examples/ajax2/ajax2.zip">here</a>.</p>
<p>More resources:<br />
<a href="http://docs.jquery.com/Tutorials">http://docs.jquery.com/Tutorials</a><br />
<a href="http://www.ibm.com/developerworks/web/library/wa-jquery1/">http://www.ibm.com/developerworks/web/library/wa-jquery1/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://danielgibbons.net/?feed=rss2&amp;p=34</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
