<?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>Two Ells &#187; ajax</title>
	<atom:link href="http://daryl.learnhouston.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://daryl.learnhouston.com</link>
	<description>Daryl&#039;s Personal Blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:35:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='daryl.learnhouston.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4d1b796395d908de139e61fff64f4900?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Two Ells &#187; ajax</title>
		<link>http://daryl.learnhouston.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://daryl.learnhouston.com/osd.xml" title="Two Ells" />
	<atom:link rel='hub' href='http://daryl.learnhouston.com/?pushpress=hub'/>
		<item>
		<title>Inviting JSON to the Table</title>
		<link>http://daryl.learnhouston.com/2006/03/07/inviting-json-to-the-table/</link>
		<comments>http://daryl.learnhouston.com/2006/03/07/inviting-json-to-the-table/#comments</comments>
		<pubDate>Tue, 07 Mar 2006 15:04:10 +0000</pubDate>
		<dc:creator>Daryl L. L. Houston</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://daryl.wordpress.com/2006/03/07/inviting-json-to-the-table</guid>
		<description><![CDATA[I'm doing some preliminary work on a project for which it's been suggested that I consider using JSON rather than XML as a data transport. "JSON?" you ask. "What's that?" It stands for JavaScript Object Notation, and for those of us who've spent a lot of time writing javascript, it'll look very familiar. It's a subset of the javascript language and can be described as the convention whereby one represents object members as name/value pairs. In short, it's a form of serialization native to javascript and is therefore understood by all modern browsers out of the box and by many other programming languages either natively or by simple extension. A javascript function can eval a JSON text string with no additional parsing needed and can then use the decoded values directly. This can be beneficial to web applications in at least two potentially notable ways:
<ul>
	<li>It eliminates the need to parse a verbose XML document into an object and then perform operations on the object.</li>
	<li>The format can be (though isn't necessarily) less verbose than XML.</li>
</ul>
Transfer time and processing overhead can therefore be optimized when using JSON in some circumstances. Furthermore, for some uses, JSON might actually save programming effort required on the server side to generate XML from objects or on the client side going in the other direction.

Those advantages notwithstanding, I was originally hesitant to give JSON more than a passing glance. For most web applications that feature the sort of functionality I had in mind (including, as far as I was aware at the time, the one I'm doing R&#38;D for), existing AJAX toolkits fit the bill, and I was inclined to use an existing AJAX toolkit rather than to implement JSON for the sheer novelty of doing so. Consider an editable grid table, for example. You have a text field with an onchange event. On change, you send a small piece of data to the server and you get a small piece of data back that tells the client how to provide UI feedback. None of the three benefits of JSON I mentioned above really apply here, as the data in both directions is small, requires almost no processing, and need not be an otherwise usable object. It's text out, text in, and minor DOM manipulation. In such a case, JSON provides no real advantage, and you might as well go with a standard AJAX toolkit.

A colleague working on the project with me pointed out some other possible use cases, however, that might render JSON worth further investigation. For example, if the data comes down as an object, it can be sorted and have calculations performed on it more readily on the client side without a round-trip to the server and back per operation. There's something very appealing to me about this. So I'll be doing more diligence on JSON.

As my first foray into coding with JSON, I wanted to test the example my colleague brought up. Doing so required me to grab a few libraries, and I haven't packaged it all up nicely, but it'd be reasonably easy to assemble these things and test this out for yourself if you're interested. Here's what you need to grab:
<ul>
	<li><a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198">Services_JSON PEAR class</a></li>
	<li><a href="http://kryogenix.org/code/browser/sorttable/sorttable.js">sorttable.js</a> (for client-side js table sorting goodness; this thing is superb and is a new find for me)</li>
	<li><a href="http://www.softwaresecretweapons.com/jspwiki/articles/JavaScriptRefactoring/new/oyXMLRPC.js">oyXMLRPC.js</a> or some other xmlrpc library for js; I modified this one to make it POST-friendly</li>
</ul>
So, in a web sandbox, save the PEAR class as JSON.php and create a file named "process.php" with the following contents:
<pre>&#60;?php

include("JSON.php");

$rows = array();
$cols = array();

$colcount=20;

for($i=0; $i&#60;$colcount; $i++){
array_push($cols, "col $i");
}

for($i=0; $i&#60;100; $i++){
$row=array("count"      =&#62; $i);
for($j=0; $j&#60;$colcount; $j++){
$row[$cols[$j] ] = substr(md5(microtime() . $cols[$colcount]),0,8);
}
array_push($rows, $row);
}

$response=array(
"error"         =&#62; "0",
"message"       =&#62; "success",
"payload"       =&#62; $rows
);

$json = new Services_JSON();
$output = $json-&#62;encode($response);
print($output);

?&#62;</pre>
This script generates 100 rows of 20 columns of junk data and returns it as a JSON object. In a real-world application, this would presumably be a data set returned from a database. The XMLHttpRequest issued from your client calls this script and handles the data. Now on to that part of the code. Create a file named index.html and populate it as follows:
<pre>&#60;html&#62;
&#60;head&#62;
&#60;title&#62;JSON Demo&#60;/title&#62;
&#60;script type="text/javascript" xsrc="sortable.js" mce_src="sortable.js"   &#62;&#60;/script&#62;
&#60;script type="text/javascript" xsrc="json.js" mce_src="json.js"   &#62;&#60;/script&#62;
&#60;script type="text/javascript" xsrc="xmlrpc.js" mce_src="xmlrpc.js"   &#62;&#60;/script&#62;
&#60;style type="text/css"&#62;
/* Sortable tables */
table.sortable a.sortheader {
background-color:#eee;
color:#666666;
font-weight: bold;
text-decoration: none;
display: block;
}
table.sortable span.sortarrow {
color: black;
text-decoration: none;
}
&#60;/style&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;div id="container"&#62;
&#60;input type="text" id="thefield" name="thefield" value="" /&#62;
&#60;input type="button" id="thebutton" name="thebutton" value="The Button" onclick="json_request(document.getElementById('thefield').value)" /&#62;
&#60;/div&#62;
&#60;/body&#62;
&#60;/html&#62;</pre>
Note the javascript includes at the top, and be sure to name the downloaded libraries appropriately or to change the file. Now create json.js and populate it as follows:
<pre>function json_request(txt){

var myOnComplete = function(responseText, responseXML){
var obj = eval('(' + responseText + ')');
var container=document.getElementById('container');
container.appendChild(make_table(obj.payload));
sortables_init();
}

var myOnLog = function(msg){
alert(msg);
}

var provider = new oyXMLRPCProvider();
provider.onComplete = myOnComplete;
provider.onError = myOnLog;

provider.submit("process.php?txt=" + escape(txt));
}

function make_table(data){
var table = document.createElement('table');
table.className='sortable';
var tbody = document.createElement('tbody');
for(var i=0; i&#60;data.length; i++ ){
var tr = document.createElement('tr');
//Create headers on first iteration.
if(i==0){
for(var field in data[i]){
var th = document.createElement('th');
th.innerHTML=field;
tr.appendChild(th);
}
tbody.appendChild(tr);
//Be sure to start a new row.
var tr = document.createElement('tr');
}
for(var field in data[i]){
var td = document.createElement('td');
td.innerHTML=data[i][field];
td.className=field;
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
table.id="table_" + Math.floor ( Math.random ( ) * 100 );
return table;
}</pre>
If you get everything linked up correctly, the result should be that when you press the button on the main page, a JSON object is pulled down asynchronously from the server and appended to the page as a table of data. Each such table is independently sortable without round trips to the server and back (and without your having to write sorting validation code to prevent SQL injection attacks, etc.). Of course, this does degrade poorly for browsers in which javascript is disabled. In any case, I've modeled one bit of functionality that's pretty painless to implement using JSON, and I suspect that further work in this direction will turn up even more interesting results.

For more information on JSON, be sure to hit the <a href="http://json.org">JSON site</a>.

<!-- technorati tags begin -->

technorati tags: <a rel="tag" href="http://technorati.com/tag/json">json</a>, <a rel="tag" href="http://technorati.com/tag/ajax">ajax</a>, <a rel="tag" href="http://technorati.com/tag/javascript">javascript</a>

<!-- technorati tags end --> <a href="http://daryl.learnhouston.com/2006/03/07/inviting-json-to-the-table/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daryl.learnhouston.com&amp;blog=7&amp;post=80&amp;subd=daryl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m doing some preliminary work on a project for which it&#8217;s been suggested that I consider using JSON rather than XML as a data transport. &#8220;JSON?&#8221; you ask. &#8220;What&#8217;s that?&#8221; It stands for JavaScript Object Notation, and for those of us who&#8217;ve spent a lot of time writing javascript, it&#8217;ll look very familiar. It&#8217;s a subset of the javascript language and can be described as the convention whereby one represents object members as name/value pairs. In short, it&#8217;s a form of serialization native to javascript and is therefore understood by all modern browsers out of the box and by many other programming languages either natively or by simple extension. A javascript function can eval a JSON text string with no additional parsing needed and can then use the decoded values directly. This can be beneficial to web applications in at least two potentially notable ways:</p>
<ul>
<li>It eliminates the need to parse a verbose XML document into an object and then perform operations on the object.</li>
<li>The format can be (though isn&#8217;t necessarily) less verbose than XML.</li>
</ul>
<p>Transfer time and processing overhead can therefore be optimized when using JSON in some circumstances. Furthermore, for some uses, JSON might actually save programming effort required on the server side to generate XML from objects or on the client side going in the other direction.</p>
<p>Those advantages notwithstanding, I was originally hesitant to give JSON more than a passing glance. For most web applications that feature the sort of functionality I had in mind (including, as far as I was aware at the time, the one I&#8217;m doing R&amp;D for), existing AJAX toolkits fit the bill, and I was inclined to use an existing AJAX toolkit rather than to implement JSON for the sheer novelty of doing so. Consider an editable grid table, for example. You have a text field with an onchange event. On change, you send a small piece of data to the server and you get a small piece of data back that tells the client how to provide UI feedback. None of the three benefits of JSON I mentioned above really apply here, as the data in both directions is small, requires almost no processing, and need not be an otherwise usable object. It&#8217;s text out, text in, and minor DOM manipulation. In such a case, JSON provides no real advantage, and you might as well go with a standard AJAX toolkit.</p>
<p>A colleague working on the project with me pointed out some other possible use cases, however, that might render JSON worth further investigation. For example, if the data comes down as an object, it can be sorted and have calculations performed on it more readily on the client side without a round-trip to the server and back per operation. There&#8217;s something very appealing to me about this. So I&#8217;ll be doing more diligence on JSON.</p>
<p>As my first foray into coding with JSON, I wanted to test the example my colleague brought up. Doing so required me to grab a few libraries, and I haven&#8217;t packaged it all up nicely, but it&#8217;d be reasonably easy to assemble these things and test this out for yourself if you&#8217;re interested. Here&#8217;s what you need to grab:</p>
<ul>
<li><a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198">Services_JSON PEAR class</a></li>
<li><a href="http://kryogenix.org/code/browser/sorttable/sorttable.js">sorttable.js</a> (for client-side js table sorting goodness; this thing is superb and is a new find for me)</li>
<li><a href="http://www.softwaresecretweapons.com/jspwiki/articles/JavaScriptRefactoring/new/oyXMLRPC.js">oyXMLRPC.js</a> or some other xmlrpc library for js; I modified this one to make it POST-friendly</li>
</ul>
<p>So, in a web sandbox, save the PEAR class as JSON.php and create a file named &#8220;process.php&#8221; with the following contents:</p>
<pre>&lt;?php

include("JSON.php");

$rows = array();
$cols = array();

$colcount=20;

for($i=0; $i&lt;$colcount; $i++){
array_push($cols, "col $i");
}

for($i=0; $i&lt;100; $i++){
$row=array("count"      =&gt; $i);
for($j=0; $j&lt;$colcount; $j++){
$row[$cols[$j] ] = substr(md5(microtime() . $cols[$colcount]),0,8);
}
array_push($rows, $row);
}

$response=array(
"error"         =&gt; "0",
"message"       =&gt; "success",
"payload"       =&gt; $rows
);

$json = new Services_JSON();
$output = $json-&gt;encode($response);
print($output);

?&gt;</pre>
<p>This script generates 100 rows of 20 columns of junk data and returns it as a JSON object. In a real-world application, this would presumably be a data set returned from a database. The XMLHttpRequest issued from your client calls this script and handles the data. Now on to that part of the code. Create a file named index.html and populate it as follows:</p>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;JSON Demo&lt;/title&gt;
&lt;script type="text/javascript" xsrc="sortable.js" mce_src="sortable.js"   &gt;&lt;/script&gt;
&lt;script type="text/javascript" xsrc="json.js" mce_src="json.js"   &gt;&lt;/script&gt;
&lt;script type="text/javascript" xsrc="xmlrpc.js" mce_src="xmlrpc.js"   &gt;&lt;/script&gt;
&lt;style type="text/css"&gt;
/* Sortable tables */
table.sortable a.sortheader {
background-color:#eee;
color:#666666;
font-weight: bold;
text-decoration: none;
display: block;
}
table.sortable span.sortarrow {
color: black;
text-decoration: none;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="container"&gt;
&lt;input type="text" id="thefield" name="thefield" value="" /&gt;
&lt;input type="button" id="thebutton" name="thebutton" value="The Button" onclick="json_request(document.getElementById('thefield').value)" /&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Note the javascript includes at the top, and be sure to name the downloaded libraries appropriately or to change the file. Now create json.js and populate it as follows:</p>
<pre>function json_request(txt){

var myOnComplete = function(responseText, responseXML){
var obj = eval('(' + responseText + ')');
var container=document.getElementById('container');
container.appendChild(make_table(obj.payload));
sortables_init();
}

var myOnLog = function(msg){
alert(msg);
}

var provider = new oyXMLRPCProvider();
provider.onComplete = myOnComplete;
provider.onError = myOnLog;

provider.submit("process.php?txt=" + escape(txt));
}

function make_table(data){
var table = document.createElement('table');
table.className='sortable';
var tbody = document.createElement('tbody');
for(var i=0; i&lt;data.length; i++ ){
var tr = document.createElement('tr');
//Create headers on first iteration.
if(i==0){
for(var field in data[i]){
var th = document.createElement('th');
th.innerHTML=field;
tr.appendChild(th);
}
tbody.appendChild(tr);
//Be sure to start a new row.
var tr = document.createElement('tr');
}
for(var field in data[i]){
var td = document.createElement('td');
td.innerHTML=data[i][field];
td.className=field;
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
table.id="table_" + Math.floor ( Math.random ( ) * 100 );
return table;
}</pre>
<p>If you get everything linked up correctly, the result should be that when you press the button on the main page, a JSON object is pulled down asynchronously from the server and appended to the page as a table of data. Each such table is independently sortable without round trips to the server and back (and without your having to write sorting validation code to prevent SQL injection attacks, etc.). Of course, this does degrade poorly for browsers in which javascript is disabled. In any case, I&#8217;ve modeled one bit of functionality that&#8217;s pretty painless to implement using JSON, and I suspect that further work in this direction will turn up even more interesting results.</p>
<p>For more information on JSON, be sure to hit the <a href="http://json.org">JSON site</a>.</p>
<p><!-- technorati tags begin --></p>
<p>technorati tags: <a rel="tag" href="http://technorati.com/tag/json">json</a>, <a rel="tag" href="http://technorati.com/tag/ajax">ajax</a>, <a rel="tag" href="http://technorati.com/tag/javascript">javascript</a></p>
<p><!-- technorati tags end --></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/daryl.wordpress.com/80/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/daryl.wordpress.com/80/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/daryl.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/daryl.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/daryl.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/daryl.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/daryl.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/daryl.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/daryl.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/daryl.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daryl.learnhouston.com&amp;blog=7&amp;post=80&amp;subd=daryl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://daryl.learnhouston.com/2006/03/07/inviting-json-to-the-table/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ea52ba884713c11a9f814dde6867c97?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Administrator</media:title>
		</media:content>
	</item>
		<item>
		<title>Xajax in Drupal</title>
		<link>http://daryl.learnhouston.com/2005/11/21/xajax-in-drupal/</link>
		<comments>http://daryl.learnhouston.com/2005/11/21/xajax-in-drupal/#comments</comments>
		<pubDate>Mon, 21 Nov 2005 08:29:03 +0000</pubDate>
		<dc:creator>Daryl L. L. Houston</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://daryl.wordpress.com/2005/11/21/xajax-in-drupal</guid>
		<description><![CDATA[I've taken a little break from tech postings here because most of my tech postings of late had been about <a href="http://flock.com">Flock</a>, and I wanted to group those elsewhere. Here's another tech posting that's relevant to Flock because I wrote this code for a project for Flock, but it doesn't exactly belong at that blog because it's not specifically about Flock. So feel free to skip over this one, dear family members.

<a href="http://xajax.sourceforge.net/">Xajax</a> is a php library that makes <a href="http://www.adaptivepath.com/publications/essays/archives/000385.php">ajax</a> development a cinch. I've looked at a few ajax libraries, and there are better implementations than what xajax provides on the javascript side, but the ease with which one correlates php function calls to javascript function calls in xajax is well worth the reduced functionality in many cases. For example, xajax doesn't (as far as I've investigated, at least) provide elegant drag and drop functionality. But for simple updating of content upon request completion, it handles the basics without forcing you to write your own javascript callback functions (which can be a pain and which can make your source code look really nasty). Here's a quick primer on how it works:

<ul>
  <li>Write php functions to do server-side data manipulation and to use xajax calls to <a href="http://xajax.sourceforge.net/#update">manipulate the page</a> upon request return.</li>
  <li>Register these functions in your php script. This gets the correlating javascript functions added to the javascript include.</li>
  <li>Include the javascript in your source, tell xajax to process requests, etc. (all this is outlined in detail at the xajax site).</li>
  <li>Add the javascript function calls to your source. If your php function was named "delete_member," your javascript call is named "xajax_delete_member."</li>
</ul>

That's it. Arguments passed to the js function are passed straight through to the php function, and you have to do no special mumbo jumbo to make the thing work. The out-of-the-box functionality is pretty simple when compared to the libraries available at, for example, <a href="http://script.aculo.us/">script.aculo.us</a>, but xajax is reportedly extensible, so you're certainly not limited to what you get out of the box.

Now, on to why this is ideal for <a href="http://drupal.org">Drupal</a> development. The more I use Drupal, the more appealing I find its API. It's pretty simple to extend the software. For example, I recently wanted to add a little snippet of code next to the title of any sort of node. Lucky for me, the Drupal developers thought to include a nodeapi hook, which lets you do just what I wanted to do. For all its niceness in some areas, though, it's painful to do some things in Drupal. Adding global ajax support seemed a likely candidate for such pain because Drupal's got menu hooks, a permissions system, etc., that I thought might make for tedious hacking.

I started by simply including the xajax code in one module I was working on (as proposed <a href="http://drupal.org/node/29035">here</a>), but when I wanted to include the same sort of functionality in another module, I started getting collisions. The library code was called more than once, so I found myself getting php errors. This prompted me to try to find a way to make xajax work globally across a Drupal install. And it turned out to be dead simple. I simply included the xajax library as in the example <a href="http://drupal.org/node/29035">linked to above</a>. If the node is enabled, this adds the javascript include to the template globally. Then I added some code that checks all installed Drupal modules for a function named MODULE_xajax_init (where "MODULE" is the module name). This init function for relevant modules performs the function registration calls. The routine for adding ajax support to any module in Drupal, then, now goes as follows:

<ul>
  <li>Install and enable xajax.module.</li>
  <li>For the module you're writing, define MODULE_xajax_init and include function registration calls.</li>
  <li>Within your module, define the php functions you've registered.</li>
  <li>Add javascript calls to your UI.</li>
</ul>

That's it. Voila. Magic, easy ajax support in Drupal.

Xajax is especially suited to Drupal because it handles everything using simple php code. There's no writing of nasty javascript callbacks and embedding those into your templates or, worse, into your module itself. The appeal of using this xajax module is that it works globally across your Drupal install with no "function already defined" type errors and, well, it's done and it's free. You can download the module <a href="http://daryl.learnhouston.com/downloads/xajax.module.tgz">here</a>. <a href="http://daryl.learnhouston.com/2005/11/21/xajax-in-drupal/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daryl.learnhouston.com&amp;blog=7&amp;post=224&amp;subd=daryl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve taken a little break from tech postings here because most of my tech postings of late had been about <a href="http://flock.com">Flock</a>, and I wanted to group those elsewhere. Here&#8217;s another tech posting that&#8217;s relevant to Flock because I wrote this code for a project for Flock, but it doesn&#8217;t exactly belong at that blog because it&#8217;s not specifically about Flock. So feel free to skip over this one, dear family members.</p>
<p><a href="http://xajax.sourceforge.net/">Xajax</a> is a php library that makes <a href="http://www.adaptivepath.com/publications/essays/archives/000385.php">ajax</a> development a cinch. I&#8217;ve looked at a few ajax libraries, and there are better implementations than what xajax provides on the javascript side, but the ease with which one correlates php function calls to javascript function calls in xajax is well worth the reduced functionality in many cases. For example, xajax doesn&#8217;t (as far as I&#8217;ve investigated, at least) provide elegant drag and drop functionality. But for simple updating of content upon request completion, it handles the basics without forcing you to write your own javascript callback functions (which can be a pain and which can make your source code look really nasty). Here&#8217;s a quick primer on how it works:</p>
<ul>
<li>Write php functions to do server-side data manipulation and to use xajax calls to <a href="http://xajax.sourceforge.net/#update">manipulate the page</a> upon request return.</li>
<li>Register these functions in your php script. This gets the correlating javascript functions added to the javascript include.</li>
<li>Include the javascript in your source, tell xajax to process requests, etc. (all this is outlined in detail at the xajax site).</li>
<li>Add the javascript function calls to your source. If your php function was named &#8220;delete_member,&#8221; your javascript call is named &#8220;xajax_delete_member.&#8221;</li>
</ul>
<p>That&#8217;s it. Arguments passed to the js function are passed straight through to the php function, and you have to do no special mumbo jumbo to make the thing work. The out-of-the-box functionality is pretty simple when compared to the libraries available at, for example, <a href="http://script.aculo.us/">script.aculo.us</a>, but xajax is reportedly extensible, so you&#8217;re certainly not limited to what you get out of the box.</p>
<p>Now, on to why this is ideal for <a href="http://drupal.org">Drupal</a> development. The more I use Drupal, the more appealing I find its API. It&#8217;s pretty simple to extend the software. For example, I recently wanted to add a little snippet of code next to the title of any sort of node. Lucky for me, the Drupal developers thought to include a nodeapi hook, which lets you do just what I wanted to do. For all its niceness in some areas, though, it&#8217;s painful to do some things in Drupal. Adding global ajax support seemed a likely candidate for such pain because Drupal&#8217;s got menu hooks, a permissions system, etc., that I thought might make for tedious hacking.</p>
<p>I started by simply including the xajax code in one module I was working on (as proposed <a href="http://drupal.org/node/29035">here</a>), but when I wanted to include the same sort of functionality in another module, I started getting collisions. The library code was called more than once, so I found myself getting php errors. This prompted me to try to find a way to make xajax work globally across a Drupal install. And it turned out to be dead simple. I simply included the xajax library as in the example <a href="http://drupal.org/node/29035">linked to above</a>. If the node is enabled, this adds the javascript include to the template globally. Then I added some code that checks all installed Drupal modules for a function named MODULE_xajax_init (where &#8220;MODULE&#8221; is the module name). This init function for relevant modules performs the function registration calls. The routine for adding ajax support to any module in Drupal, then, now goes as follows:</p>
<ul>
<li>Install and enable xajax.module.</li>
<li>For the module you&#8217;re writing, define MODULE_xajax_init and include function registration calls.</li>
<li>Within your module, define the php functions you&#8217;ve registered.</li>
<li>Add javascript calls to your UI.</li>
</ul>
<p>That&#8217;s it. Voila. Magic, easy ajax support in Drupal.</p>
<p>Xajax is especially suited to Drupal because it handles everything using simple php code. There&#8217;s no writing of nasty javascript callbacks and embedding those into your templates or, worse, into your module itself. The appeal of using this xajax module is that it works globally across your Drupal install with no &#8220;function already defined&#8221; type errors and, well, it&#8217;s done and it&#8217;s free. You can download the module <a href="http://daryl.learnhouston.com/downloads/xajax.module.tgz">here</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/daryl.wordpress.com/224/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/daryl.wordpress.com/224/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/daryl.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/daryl.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/daryl.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/daryl.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/daryl.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/daryl.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/daryl.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/daryl.wordpress.com/224/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daryl.learnhouston.com&amp;blog=7&amp;post=224&amp;subd=daryl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://daryl.learnhouston.com/2005/11/21/xajax-in-drupal/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6ea52ba884713c11a9f814dde6867c97?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Administrator</media:title>
		</media:content>
	</item>
	</channel>
</rss>
