<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Xylot Design &#187; primer</title>
	<atom:link href="http://xylotdesign.wordpress.com/tag/primer/feed/" rel="self" type="application/rss+xml" />
	<link>http://xylotdesign.wordpress.com</link>
	<description>useful scripts, ideas, and tips!</description>
	<lastBuildDate>Fri, 21 Mar 2008 20:39:45 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='xylotdesign.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/d47e221117c84d128bbe00e39a6140a7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Xylot Design &#187; primer</title>
		<link>http://xylotdesign.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://xylotdesign.wordpress.com/osd.xml" title="Xylot Design" />
		<item>
		<title>AJAX: A Primer</title>
		<link>http://xylotdesign.wordpress.com/2008/03/14/ajax-a-primer/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/14/ajax-a-primer/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 18:33:07 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[primer]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=5</guid>
		<description><![CDATA[First off, what is AJAX? AJAX stands for Asynchronous JavaScript And XML. What does that mean? Basically, it means that you can call up other scripts and retrieve dynamically generated content (or perform simple actions) without ever leaving the current page. It&#8217;s how a large number of sites let you log in nowadays, and on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=5&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>First off, what is AJAX? AJAX stands for <u><b>A</b></u>synchronous <u><b>J</b></u>avaScript <u><b>A</b></u>nd <u><b>X</b></u>ML. What does that mean? Basically, it means that you can call up other scripts and retrieve dynamically generated content (or perform simple actions) without ever leaving the current page. It&#8217;s how a large number of sites let you log in nowadays, and on what Yahoo! Mail Beta is based. It became popular through <a href="http://www.google.com/webhp?complete=1&amp;hl=en" target="_blank">Google Suggest</a>, where AJAX is the framework behind that little list that pops up below the search bar while you type in your query.<span id="more-5"></span></p>
<div style="text-align:center;"><img src="http://img.xylot.net/googlesuggest.jpg" alt="Google Suggest screenshot" border="0" height="350" width="400" /></div>
<p>In Google Suggest, AJAX calls up an external script while you type, looking through a list of popular search queries, and return those that start with the text you are typing, providing suggestions for your search. It is quite useful in that it allows the visitor to see a search query that might return more (or better) results than the one the visitor had in mind.</p>
<p>AJAX isn&#8217;t a programming language in itself; it is simply a method of combining JavaScript and the dynamic programming language of your choice (ASP, PHP, etc.) to run another page&#8217;s server-side script. There are two parts to an AJAX script: the originating page, where the user is located; and the external page, containing the script that AJAX calls.</p>
<p>We&#8217;re going to create our own little page with a &#8220;name&#8221; text field. When somebody types text into that field, the page will call up an external script that returns the current time. (Sure, we can use plain JS to do this, but this is just a demonstration!)</p>
<p>To start off, we will create the HTML page with the form.</p>
<pre>&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;AJAX Test Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form name="testform"&gt;
            Name: &lt;input type="text" name="inputname" /&gt;&lt;br /&gt;
            Time: &lt;input type="text" name="currtime" /&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Pretty basic, right? So far we have the text &#8220;Name&#8221;, followed by a text field in which the user can enter text. Nothing else happens at the moment, as you can see.</p>
<p>We need to call up our script when the user types in text. The best handler to use for this is the <b>onkeyup</b> event handler. Add the event handler and have it call up a function we will then create:</p>
<pre>&lt;input type="text" name="inputname" onkeyup="getTime()" /&gt;</pre>
<p>Different browsers use different methods to create the XML-HTTP Request object we will need. Internet Explorer uses an ActiveX object, while most other browsers use a built-in JavaScript object. We will use <b>try</b> and <b>catch</b> statements to generate the correct object.</p>
<p>Let&#8217;s define the function that will return the XMLHttpRequest object. In the <b>&lt;head&gt;</b> section, add some JavaScript:</p>
<pre>&lt;script language="javascript" type="text/javascript"&gt;
    function getXHObject() {
        var xmlHttp = null;
        try {
            // For Firefox, Opera 8.0+, and Safari:
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            // Internet Explorer (below 7.0, and 7.0+):
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
&lt;/script&gt;</pre>
<p>The above script will attempt to create the built-in JS object, which will work for most non-IE browsers. If that doesn&#8217;t work, then it tries to call up one of the IE-supported methods. The first one works with versions of IE from 5.5 to under 7.0. The second one works with IE 7.0+. Finally, it returns the XML-HTTP object.</p>
<p>What if none of them work? In that case, the user&#8217;s browser is incredibly obsolete and does not support AJAX at all. Sure, this isn&#8217;t 100% compatible with all browsers, but so few people use ancient web browsers that it doesn&#8217;t matter much anyway. It comes to the point where they really should upgrade anyway, to be totally honest.</p>
<p>Next, we will create the <b>getTime()</b> function, which will call up the external script and retrieve the needed information.</p>
<pre>var xmlHttp;    // This var must be global
function getTime() {
    // If the input mattered, we would check for blank or invalid input here
    // Create XML-HTTP object
    xmlHttp = getXHObject();
    // Check if null - browser is obselete
    if (xmlHttp == null) {
        alert('Your browser does not support AJAX!');
        return;
    }
    // The URL we will call - if input mattered, we would add a query string of some sort.
    var url = 'gettime.php';
    // Required AJAX code
    xmlHttp.onreadystatechange = stateChanged; // Specify function that will output info.
    xmlHttp.open('GET', url, true);            // Open the URL for sending...
    xmlHttp.send(null);                        // ...and send the request.
}</pre>
<p>If the input affected the output (such as getting information based on user input), then the above <b>getTime()</b> function would have included the input as a parameter, and we would have checked the input to make sure it was valid. We would also have generated a URL with the input in the query string, such as <b>getusername.php?id=123</b>.</p>
<p>Next, we will define the <b>stateChanged()</b> function, which is called while the XML-HTTP request is working.</p>
<pre>function stateChanged() {
    if (xmlHttp.readyState == 4) {
        // Output was received
        document.testform.currtime.value = xmlHttp.responseText;
    } else {
        // Nothing was received yet, but the XML-HTTP request is working.
        // We may want to generate a message that asks the user to wait.
    }
}</pre>
<p>The above function will set the <b>currtime</b> text field&#8217;s value to the response text — the output received from the <b>gettime.php</b> external script.</p>
<p>Finally, we will set up the <b>gettime.php</b> page itself. Whatever we <b>echo</b> out will be the response text when called via AJAX. In a new file, we will use PHP to get the current time.</p>
<pre>&lt;?php
    echo date('g:i:s A');
    // Return something like "1:45:23 AM"
?&gt;</pre>
<p>And that&#8217;s it!</p>
<p>Basically, here&#8217;s how it works: The user types in a value into the &#8220;Name&#8221; field. After each keystroke, the <b>getTime()</b> function is called and creates an XML-HTTP object. This is then used to communicate silently with <b>gettime.php</b>, which outputs the time. This output triggers a Ready State of <b>4</b>. The <b>stateChanged()</b> function is then used to take the output and put it into the &#8220;Time&#8221; text field.</p>
<p>Now you know how to utilize AJAX, and have just increased your web development arsenal. As stated above, you can have output that changes depending on the user input. For example, you could have a text field that receives a User ID as input, and the external script will take that ID, query a database, and return that user&#8217;s full name, or some other piece of information.</p>
<p>Furthermore, instead of using <b>onkeyup</b> as the event handler, you could have a button click (<b>onclick</b>) or a click away from the text field (<b>onblur</b>) act as the trigger.</p>
<p>On a side note, I promise that future posts will not be as long as this one. I just wanted to get an AJAX primer out of the way, kind of as our starting big bang.</p>
<p><i>Happy coding!</i></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=5&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/14/ajax-a-primer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>

		<media:content url="http://img.xylot.net/googlesuggest.jpg" medium="image">
			<media:title type="html">Google Suggest screenshot</media:title>
		</media:content>
	</item>
	</channel>
</rss>