<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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" version="2.0">

<channel>
	<title>Graphics by Greg</title>
	
	<link>http://www.gregphoto.net</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Tue, 28 Oct 2008 05:10:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/GraphicsByGreg" type="application/rss+xml" /><item>
		<title>A walk on the beach</title>
		<link>http://www.gregphoto.net/index.php/2008/10/19/a-walk-on-the-beach/</link>
		<comments>http://www.gregphoto.net/index.php/2008/10/19/a-walk-on-the-beach/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 23:36:40 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/?p=49</guid>
		<description><![CDATA[Its been a long time since I&#8217;ve written anything here&#8230;mostly due to a very busy year which included Yenari and me getting married in June.  I&#8217;ll try my best to be more active (i.e. more than one post a year), but for now, I&#8217;ll just provide a link to a little experiment I put [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a long time since I&#8217;ve written anything here&#8230;mostly due to a very busy year which included Yenari and me getting married in June.  I&#8217;ll try my best to be more active (i.e. more than one post a year), but for now, I&#8217;ll just provide a link to a little experiment I put together from some pictures from our honeymoon in the Cook Islands.  It&#8217;s a little slideshow-like page, built with JavaScript:</p>
<p><a href="http://www.gregphoto.net/projects/beach">Walking into the Ocean</a></p>
<p>Nothing too crazy on the JavaScript side - just playing around with some stuff I hadn&#8217;t done before.  I wrote about three quarters of this a few months ago and just finished it off this afternoon.  I had to write a simple, but hopefully effective, image preloader that has nice callbacks and a decent api.  The actual slideshow could be written better, but it works, and that&#8217;s all I was going for&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2008/10/19/a-walk-on-the-beach/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Yelp for the iPhone</title>
		<link>http://www.gregphoto.net/index.php/2007/12/03/yelp-for-the-iphone/</link>
		<comments>http://www.gregphoto.net/index.php/2007/12/03/yelp-for-the-iphone/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 05:51:12 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2007/12/03/yelp-for-the-iphone/</guid>
		<description><![CDATA[I just put together my first simple website for my new toy - my iPhone.  It is a simple iPhone-specific interface for Yelp, my favorite source for restaurant and other local reviews. They have a ton of reviews for just about every local restaurant so I find myself using it all the time to [...]]]></description>
			<content:encoded><![CDATA[<p>I just put together <a href="http://www.gregphoto.net/projects/yelp">my first simple website</a> for my new toy - my iPhone.  It is a simple iPhone-specific interface for <a href="http://www.yelp.com">Yelp</a>, my favorite source for restaurant and other local reviews. They have a ton of reviews for just about every local restaurant so I find myself using it all the time to find new restaurants to go to or to find more info about restaurants I already know.  I could access the website just fine already on my phone, but it was a bit crowded and difficult to navigate on a small screen, so I decided it take this as a challenge to build my first iPhone web interface.</p>
<p>And yes, I&#8217;m probably about the 58th person to build a Yelp interface for the iPhone, but I was looking for something I&#8217;d actually use and learn from!</p>
<p>Yelp provides a REST search API that allows you to pass in a location and a search term and they return a JSON object with a list of matching businesses, their ratings, and the business info.  I used the Zend Framework and the Prototype JavaScript library to put it together.  I could have done a pure JavaScript implementation using script hacks, but figured it&#8217;d be better to have the PHP code serve as a basic proxy for the Yelp web service.</p>
<p><a href="http://www.gregphoto.net/projects/yelp">Try it out!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2007/12/03/yelp-for-the-iphone/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP5 Defensio Class</title>
		<link>http://www.gregphoto.net/index.php/2007/11/24/php5-defensio-class/</link>
		<comments>http://www.gregphoto.net/index.php/2007/11/24/php5-defensio-class/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 23:46:32 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2007/11/24/php5-defensio-class/</guid>
		<description><![CDATA[Looooong time since I've written anything here - about 9 months!  This time around I've put together a simple PHP5 class for communicating with the Defensio web service.  Defensio has an example PHP class, but unfortunately it is PHP4 only and it's more fun to rewrite it, right?
For those who don't know about [...]]]></description>
			<content:encoded><![CDATA[<p>Looooong time since I've written anything here - about 9 months!  This time around I've put together a simple PHP5 class for communicating with the <a href="http://www.defensio.com">Defensio</a> web service.  Defensio has an example PHP class, but unfortunately it is PHP4 only and it's more fun to rewrite it, right?</p>
<p>For those who don't know about Defensio, it is a web service for determining whether a comment on a blog or message in an application is spam.  To use it, you must first <a href="http://defensio.com/signup">register</a> for an API key.  Defensio is very similar to the venerable Akismet (see <a href="http://www.gregphoto.net/index.php/2006/06/11/akismet-and-the-zend-framework/">the article I wrote on Akismet</a>) but they provide a couple other features such an indication of the 'spaminess' of an individual message.  Akismet has worked out great for me (123,419 spam comments caught so far!), but I figured it would be good to try out something new as well.</p>
<p>The class is a PHP5 class and the public methods are reasonably well commented.  The bottom of this post has a link to a test application and a download of the class/test application.  So...the usage of this class is quite straightforward, as shown below:</p>
<div class="igBar"><span id="lphp-2"><a href="#" onclick="javascript:showCodeTxt('php-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-2">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'library/Gregphoto/Defensio.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'library/Gregphoto/Defensio/Adapter/Streams.php);</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">$apiKey = '</span>mysecretapikey<span style="color:#FF0000;">';</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">$siteUrl = '</span>http:<span style="color:#FF9933; font-style:italic;">//my.secret.site.url.com';</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$defensio</span> = <span style="color:#000000; font-weight:bold;">new</span> Gregphoto_Defensio<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$apiKey</span>,<span style="color:#0000FF;">$siteUrl</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$defensio</span>-&gt;<span style="color:#006600;">setHttpClient</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">new</span> Gregphoto_Defensio_Adapter_Streams<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$params</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'user-ip'</span> =&gt; <span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'REMOTE_ADDR'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'article-date'</span> =&gt; <span style="color:#FF0000;">'2007/11/24'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'comment-author'</span> =&gt; <span style="color:#FF0000;">'Big Bad Spammer'</span>,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'comment-type'</span> =&gt; <span style="color:#FF0000;">'comment'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'comment-content'</span> =&gt; <span style="color:#FF0000;">'please click links and buy viagra'</span>,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'comment-author-email'</span> =&gt; <span style="color:#FF0000;">'bigbadspammer@annoying.com'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'comment-author-url'</span> =&gt; <span style="color:#FF0000;">'http://www.annoying.com'</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$result</span> = <span style="color:#0000FF;">$defensio</span>-&gt;<span style="color:#006600;">audit_comment</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$result</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'spam'</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#FF0000;">'true'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Comment is spam with a spam score of "</span> . <span style="color:#0000FF;">$result</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'spaminess'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Comment is not spam"</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>A couple key points on usage:</p>
<ul>
<li>Each Gregphoto_Defensio object requires an Http adapter that it will use to make Http POST requests to the Defensio web service.  I originally hardcoded it to use the Zend_Http_Client from the Zend Framework, but figured this could discourage people from using it.  Then I wrote up a quick adapter to use PHP's Http Stream Wrappers, but realized that didn't work on my Dreamhost account, so I added in an extra one for the Curl extension.  In theory I should have added a Gregphoto_Defensio_Adapter_Interface class, but I was lazy and didn't want to add in an extra file.</li>
<li>Each Gregphoto_Defensio object can be used to make many requests</li>
<li>All of the current <a href="http://www.defensio.com/api">Defensio API's</a> are covered by the class.  These are covered by the following methods in the Gregphoto_Defensio class:
<ul>
<li>validate_key</li>
<li>announce_article</li>
<li>audit_comment</li>
<li>report_false_negatives</li>
<li>report_false_positives</li>
<li>get_stats</li>
</ul>
</li>
<li>Each of these methods takes a single parameter, an associative array of options as defined by the Defensio API.  Each API also returns an associative array of response parameters.  Two static utility methods Gregphoto_Defensio::getActions (returns a list of defensio methods such as 'validate-key') and Gregphoto_Defensio::getActionDetails (returns a list of required parameters, optional parameters, and response parameters for a specific Defensio action) are provided in order to get the details of the input and output parameters.
</ul>
<p>I created a simple <a href="http://www.gregphoto.net/projects/defensio/test">test application</a> which can be used to test Gregphoto_Defensio and all of the APIs provided by Defensio.</p>
<p><a href="http://www.gregphoto.net/projects/defensio/Gregphoto_Defensio.zip">Download the Gregphoto_Defensio class and the test application</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2007/11/24/php5-defensio-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Photos from South Korea, India, and Singapore</title>
		<link>http://www.gregphoto.net/index.php/2007/06/10/photos-from-south-korea-india-and-singapore/</link>
		<comments>http://www.gregphoto.net/index.php/2007/06/10/photos-from-south-korea-india-and-singapore/#comments</comments>
		<pubDate>Mon, 11 Jun 2007 00:57:30 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2007/06/10/photos-from-south-korea-india-and-singapore/</guid>
		<description><![CDATA[I just got back from 20 days of traveling to South Korea, India, and Singapore.  I went to South Korea with Yenari to see her family and friends for about a week.  After this I headed to Bangalore (India) for work while Yenari stayed in South Korea a bit longer.  On the [...]]]></description>
			<content:encoded><![CDATA[<p>I just got back from 20 days of traveling to South Korea, India, and Singapore.  I went to South Korea with Yenari to see her family and friends for about a week.  After this I headed to Bangalore (India) for <a href="http://www.stratify.com">work</a> while Yenari stayed in South Korea a bit longer.  On the way back home I had a long layover (9 hours) in Singapore and got a chance to check out the city.</p>
<p>Check out my pictures here: <a href="http://picasaweb.google.com/gneustaetter/SouthKoreaIndiaAndSingapore">http://picasaweb.google.com/gneustaetter/SouthKoreaIndiaAndSingapore</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2007/06/10/photos-from-south-korea-india-and-singapore/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Announcing Gregphoto_Image - a PHP5/GD2 Thumbnail Class</title>
		<link>http://www.gregphoto.net/index.php/2007/02/03/announcing-gregphoto_image-a-php5gd2-thumbnail-class/</link>
		<comments>http://www.gregphoto.net/index.php/2007/02/03/announcing-gregphoto_image-a-php5gd2-thumbnail-class/#comments</comments>
		<pubDate>Sat, 03 Feb 2007 21:41:41 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2007/02/03/announcing-gregphoto_image-a-php5gd2-thumbnail-class/</guid>
		<description><![CDATA[I put together a simple, lightweight PHP class for generating thumbnail images.  The class is compatible with PHP5 and uses the GD2 extension (included by default with PHP5) to create JPEG, PNG, and GIF thumbnails.  I've setup a Google Code project for it: Gregphoto_Image where you can checkout the source from SVN and [...]]]></description>
			<content:encoded><![CDATA[<p>I put together a simple, lightweight PHP class for generating thumbnail images.  The class is compatible with PHP5 and uses the GD2 extension (included by default with PHP5) to create JPEG, PNG, and GIF thumbnails.  I've setup a <a href="http://code.google.com/hosting">Google Code</a> project for it: <a href="http://code.google.com/p/gregphotoimage/">Gregphoto_Image</a> where you can checkout the source from SVN and file bugs.</p>
<p>The class has the following basic features:</p>
<ul>
<li>Ability to read JPEG, PNG, or GIF images</li>
<li>Ability to output JPEG, PNG, or GIF images</li>
<li>4 modes of thumbnail creation
<ul>
<li><i>MAX_HEIGHT</i> - you specify a maximum height and the dimensions are calculated based off of the height</li>
<li><i>MAX_WIDTH</i> - you specify a maximum width and the dimensions are calculated based off of the width</li>
<li><i>BEST_FIT</i> - you specify a maximum height and width and the dimensions are calculated so that the thumbnail<br />
	    is as large as possible without exceeding the maximum height or width</li>
<li><i>EXACT</i> - you specify a maximum height and width and these are directly used.  Causes distortion if the<br />
	    chosen aspect ratio is different from the aspect ratio of the image</li>
</ul>
</li>
<li>Renders/saves images in their input format by default, but allows changing the format.  For example, input a GIF but output a PNG</li>
<li>Fully documented object oriented code</li>
<li>Fluent interface for creating thumbnails with a minimal amount of code</li>
</ul>
<p>The class is licensed under the MIT license, which basically means it can be used and modified by anyone - for personal or commercial use.</p>
<p>You can <a href="http://code.google.com/p/gregphotoimage/">Download it</a> from the project page on Google Code.  You can <a href="http://www.gregphoto.net/projects/gregphoto_image/samples">view examples</a> of it running on my site - the examples are checked into SVN and can be viewed on the project site.  You can also <a href="http://www.gregphoto.net/projects/gregphoto_image/docs">view the docs</a>.</p>
<p>Example usage:</p>
<div class="igBar"><span id="lphp-6"><a href="#" onclick="javascript:showCodeTxt('php-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-6">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'path/to/Gregphoto_Image.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span> = <span style="color:#000000; font-weight:bold;">new</span> Gregphoto_Image<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'path/to/sample/image.jpg'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setMaxHeight</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setMaxWidth</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setJpegQuality</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">90</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span>Gregphoto_Image::<span style="color:#006600;">BEST_FIT</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">showThumbnail</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div class="igBar"><span id="lphp-7"><a href="#" onclick="javascript:showCodeTxt('php-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-7">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span> = <span style="color:#000000; font-weight:bold;">new</span> Gregphoto_Image<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'../images/fan.jpg'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setMaxHeight</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>-&gt;<span style="color:#006600;">setJpegQuality</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">90</span><span style="color:#006600; font-weight:bold;">&#41;</span>-&gt;<span style="color:#006600;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>-&gt;<span style="color:#006600;">showThumbnail</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showCodeTxt('php-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'path/to/Gregphoto_Image.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span> = <span style="color:#000000; font-weight:bold;">new</span> Gregphoto_Image<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'path/to/image.jpg'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setMaxHeight</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setMaxWidth</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setJpegQuality</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">90</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">setOutputType</span><span style="color:#006600; font-weight:bold;">&#40;</span>IMAGETYPE_PNG<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">resize</span><span style="color:#006600; font-weight:bold;">&#40;</span>Gregphoto_Image::<span style="color:#006600;">BEST_FIT</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$image</span>-&gt;<span style="color:#006600;">saveThumbnail</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'path/to/thumbnail.png'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2007/02/03/announcing-gregphoto_image-a-php5gd2-thumbnail-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Scriptaculous Sortables with Ajax Callback</title>
		<link>http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/</link>
		<comments>http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/#comments</comments>
		<pubDate>Wed, 17 Jan 2007 06:15:34 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/</guid>
		<description><![CDATA[I haven't written anything in a long time so I figured it is time to write something up.  Because a huge portion of my traffic seems to be related to my scriptaculous examples I've put up, I figured I'd add another one that covers the process of updating a database with the results of [...]]]></description>
			<content:encoded><![CDATA[<p>I haven't written anything in a long time so I figured it is time to write something up.  Because a huge portion of my traffic seems to be related to my <a href="http://script.aculo.us">scriptaculous </a>examples I've put up, I figured I'd add another one that covers the process of updating a database with the results of drag/drop with an Ajax call.  Each time the user changes an item's position in the list it updates the database.</p>
<p><a href="http://www.gregphoto.net/projects/scriptaculous17">See the example</a> (note: example uses Scriptaculous 1.7 Beta 2)</p>
<p>The methodology behind this is quite simple:</p>
<ol>
<li>Load the list values from the DB and show them on the screen</li>
<li>Using JavaScript, setup the sortables</li>
<li>Add a JavaScript callback to the drag/drop that points to a function that serializes the list and sends an Ajax request to the server</li>
<li>On the server, loop through the list and update the database with the proper order</li>
</ol>
<p>To do this I created three main files:</p>
<ul>
<li>index.php - the UI that shows the list and has the JavaScript</li>
<li>ajax.php - the page that processes the Ajax request</li>
<li>db.php - a simple database class included in both index.php and ajax.php that makes the connection to the DB and has methods to get the current list and to update the list</li>
</ul>
<h3>Reviewing Index.php</h3>
<p>Index.php looks like this:</p>
<div class="igBar"><span id="lphp-12"><a href="#" onclick="javascript:showCodeTxt('php-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-12">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'db.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$demo</span> = <span style="color:#000000; font-weight:bold;">new</span> SortableExample<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$list</span> = <span style="color:#0000FF;">$demo</span>-&gt;<span style="color:#006600;">getList</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;!DOCTYPE HTML PUBLIC <span style="color:#FF0000;">"-//W3C//DTD HTML 4.01//EN"</span> <span style="color:#FF0000;">"http://www.w3.org/TR/html4/strict.dtd"</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;html&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;title&gt;Scriptaculous <span style="color:#CC66CC;color:#800000;">1</span>.<span style="color:#CC66CC;color:#800000;">7</span> Sortables Ajax Example&lt;/title&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;link rel=<span style="color:#FF0000;">"stylesheet"</span> type=<span style="color:#FF0000;">"text/css"</span> href=<span style="color:#FF0000;">"style.css"</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;script src=<span style="color:#FF0000;">"js/prototype.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;script src=<span style="color:#FF0000;">"js/scriptaculous.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;script&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Event.observe<span style="color:#006600; font-weight:bold;">&#40;</span>window,<span style="color:#FF0000;">'load'</span>,init,<span style="color:#000000; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> init<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sortable.create<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'listContainer'</span>,<span style="color:#006600; font-weight:bold;">&#123;</span>tag:<span style="color:#FF0000;">'div'</span>,onUpdate:updateList<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> updateList<span style="color:#006600; font-weight:bold;">&#40;</span>container<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">var</span> url = <span style="color:#FF0000;">'ajax.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">var</span> params = Sortable.<a href="http://www.php.net/serialize"><span style="color:#000066;">serialize</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>container.id<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">var</span> ajax = <span style="color:#000000; font-weight:bold;">new</span> Ajax.Request<span style="color:#006600; font-weight:bold;">&#40;</span>url,<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: <span style="color:#FF0000;">'post'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameters: params,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onLoading: <span style="color:#000000; font-weight:bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span>$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'workingMsg'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.show<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onLoaded: <span style="color:#000000; font-weight:bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span>$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'workingMsg'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.hide<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> handleResponse<span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// this function will fire after the ajax request is complete...but we have nothing to do here</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;/script&gt;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;body&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;h2&gt;Scriptaculous Sortables Demo with Ajax Callback&lt;/h2&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Built with Scriptaculous <span style="color:#CC66CC;color:#800000;">1</span>.<span style="color:#CC66CC;color:#800000;">7</span> Beta <span style="color:#CC66CC;color:#800000;">2</span>.&nbsp; Drag items in the <a href="http://www.php.net/list"><span style="color:#000066;">list</span></a> below.&nbsp; <a href="http://www.php.net/each"><span style="color:#000066;">Each</span></a> <a href="http://www.php.net/time"><span style="color:#000066;">time</span></a> you update the <a href="http://www.php.net/list"><span style="color:#000066;">list</span></a> an Ajax call is made</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">that updated the database with the <span style="color:#000000; font-weight:bold;">new</span> order.&lt;br&gt;&lt;br&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;div id=<span style="color:#FF0000;">"listContainer"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$list</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$item</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span style="color:#FF0000;">"item_&lt;?=$item['catid'];?&gt;"</span>&gt;&lt;?=<span style="color:#0000FF;">$item</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'category'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;?&gt;&lt;/div&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/div&gt;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;div id=<span style="color:#FF0000;">"workingMsg"</span> style=<span style="color:#FF0000;">"display:none;"</span>&gt;Updating database...&lt;/div&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Let's look at this in reverse order.  At the bottom of the file there is a div with id 'containerDiv' (line 36).  This is the container for our list.  Within this is a small snippet of PHP that loops through an associative array and prints out each list item as a div with id 'item_12' where 12 is the ID for that record in the database (line 40).  Lines 2-4 contain the PHP code that include the db.php file and use it's class to get the associative array that is used here.</p>
<p>Underneath the container div is another div that has the text 'Updating database...' - this div is hidden by default and will be shown while Ajax calls are in progress (line 45).</p>
<p>Now to the JavaScript...it starts off on line 14 with a call to Event.observe that tells the browser to call the init function after the page loads.  The init function (line 15) creates a new sortable list on the listContainer element, setting all divs within it to be sortable and setting the updateList function as a callback whenever the list is updated.  This means that when the order of items in the list change, this function will be called with the list element passed as an argument.  This function will perform the Ajax request to update the database.</p>
<p>The updateList function (line 18) first sets a simple variable for the url used for the Ajax request.  Next (line 20) it calls Sortable.serialize, passing the id of the container as an argument, which serializes the list to a format such as listContainer[]=5,listContainer[]=7,listContainer[]=2 - here, the first three items of the list would be the items 5, 7, and 2 and the divs representing these items would have the following id values: item_5, item_7, and item_2.  This serialized list is set in the variable 'params'.  Next, on line 21, an Ajax request is opened with Prototype's Ajax.Request class.  It is called with two arguments, the url and an object with various options.  The options include the type of request (POST in this example, parameters to pass (the params variable constructed on line 20), and two functions (onLoading/onLoaded) that handle the showing and hiding of the 'Updating database...' div.  Normally in an Ajax request I'd also add a callback for onComplete to handle the response from the server, but in this case the Ajax response doesn't really matter to me because it is really only a one way communication in this demo - in a real application I'd have more error handling and would need to handle error conditions.</p>
<p>That's about it for the index page...not too bad, right?</p>
<h3>Reviewing ajax.php</h3>
<p>The file ajax.php is a simple file that is designed to receive the Ajax request and call the appropriate methods defined in db.php to update the list.  Its contents are shown below:</p>
<div class="igBar"><span id="lphp-13"><a href="#" onclick="javascript:showCodeTxt('php-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-13">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/session_start"><span style="color:#000066;">session_start</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'db.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$demo</span> = <span style="color:#000000; font-weight:bold;">new</span> SortableExample<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$demo</span>-&gt;<span style="color:#006600;">updateList</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'listContainer'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Yup, that's it.  Basically it starts off by calling session_start() - I typically call session_start() at the top of pages like this because starting the session sends the right http headers to prevent the file from being cached and I'm too lazy to look up what the right headers are!  Next it requires the db.php file, instantiates an object, and calls the updateList method of the object, passing the listContainer variable from the POST request (containing our serialized list).  That's it for the ajax.php page.</p>
<h3>Reviewing db.php</h3>
<p>The file db.php is a simple database class that connects to the database and has methods to get the list and to update the order of the list.  In a 'real' application I would definitely have a much cleaner implementation of the database access and would use other DB libraries to connect to the db - I used the simple mysql db functions to minimize on the number of lines needed here.  Here are the contents of the file:</p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showCodeTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> SortableExample <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; protected <span style="color:#0000FF;">$conn</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; protected <span style="color:#0000FF;">$user</span> = <span style="color:#FF0000;">'test'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; protected <span style="color:#0000FF;">$pass</span> = <span style="color:#FF0000;">'test'</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; protected <span style="color:#0000FF;">$dbname</span> = <span style="color:#FF0000;">'scriptaculous'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; protected <span style="color:#0000FF;">$host</span> = <span style="color:#FF0000;">'localhost'</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">conn</span> = <a href="http://www.php.net/mysql_connect"><span style="color:#000066;">mysql_connect</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">host</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">user</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pass</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/mysql_select_db"><span style="color:#000066;">mysql_select_db</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">dbname</span>,<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">conn</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> getList<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"SELECT * FROM categories ORDER BY orderid"</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$recordSet</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span>,<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">conn</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$results</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$recordSet</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$results</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$row</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$results</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> updateList<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$orderArray</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$orderid</span> = <span style="color:#CC66CC;color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$orderArray</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$catid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$catid</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>int<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0000FF;">$catid</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"UPDATE categories SET orderid={$orderid} WHERE catid={$catid}"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$recordSet</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span>,<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">conn</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$orderid</span>++;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Important note - this file uses PHP 5 syntax in this object...you'd need some changes to get this to run in PHP 4.  I'll skip talking about the constructor (which connects to the database) and the getList function (which...gets the list) in order to focus on the last method, 'updateList'.  This function takes in an array of items, i.e. array(5,7,2), that come from the serialized list.  It loops through the list and generates SQL UPDATE queries for each item.  Each time it increments $orderid so that the first item has an orderid of 1, the 2nd an orderid of 2, etc.  This means that whenever we get the list, as long as the SQL 'ORDER BY orderid' clause is used the list will show in the correct order.</p>
<h3>Summary</h3>
<p>That's it...hope you enjoyed it.  The hopeful moral of the story is...Ajax is easy, and so is drag and drop....especially if you're using a good library.  Personally I think that Prototype is incredibly useful for JavaScript development and Scriptaculous is a good sidekick for effects and drag/drop.  I've also used the <a href="http://developer.yahoo.com/yui">Yahoo YUI widgets</a> at work where we needed to do some more dynamic widgets such as trees - it worked really well.  <a href="http://www.jackslocum.com">Jack Slocum's YUI extension</a> is incredibly well done too...though it's a heavy one in terms of file size!  In the end, it comes down to finding the right tool for the job...</p>
<p><a href="http://www.gregphoto.net/projects/scriptaculous17/scriptaculous_ajax.zip">Download </a>the sample application if you want to take a closer look....</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating an Accordian Widget Class with Prototype</title>
		<link>http://www.gregphoto.net/index.php/2006/08/30/creating-an-accordian-widget-class-with-prototype/</link>
		<comments>http://www.gregphoto.net/index.php/2006/08/30/creating-an-accordian-widget-class-with-prototype/#comments</comments>
		<pubDate>Thu, 31 Aug 2006 04:24:25 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2006/08/30/creating-an-accordian-widget-class-with-prototype/</guid>
		<description><![CDATA[Overview
The following is an example of a simple reusable widget created using the Prototype JavaScript library.  The widget is an 'accordian' - a widget composed of multiple sections, of which only one is open at a time. This type of widget is available in all sorts of applications, from Microsoft Outlook (circa Outlook 2000), [...]]]></description>
			<content:encoded><![CDATA[<h3>Overview</h3>
<p>The following is an example of a simple reusable widget created using the <a href="http://prototype.conio.net/">Prototype</a> JavaScript library.  The widget is an 'accordian' - a widget composed of multiple sections, of which only one is open at a time. This type of widget is available in all sorts of applications, from Microsoft Outlook (circa Outlook 2000), to web applications, and more.  In addition to showing off this simple widget, I'd like to walk through it to explain how it works and how to write something like this with Prototype.  For all of Prototype's greatness, there is certainly a lack of documentation that makes it a little bit difficult to get into.</p>
<h3>Example</h3>
<p>Take a look at the <a href="http://www.gregphoto.net/projects/accordian">example page</a> I put together showing a few examples of accordians and their capabilities.  This article will explain how to put together the class that does this.  </p>
<h3>Explaination: Creating Reusable Classes</h3>
<p>I'll skip with going through the HTML - you can see that on the example page if you're interested - it would be helpful in understanding the javaScript here.  In this example I've used version 1.5.0_rc0 of Prototype.  The first line of the accordian.js file has the following:</p>
<div class="igBar"><span id="ljavascript-23"><a href="#" onclick="javascript:showCodeTxt('javascript-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-23">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Accordian = <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Prototype's Class.create() sets up Accordian as an object that can be instantiated.  When the object is instantiated the 'initialize' method, its constructor, will be automatically called.  You setup all of the methods within the object by defining its prototype - an object with properties and methods.  This could be done in the following way:</p>
<div class="igBar"><span id="ljavascript-24"><a href="#" onclick="javascript:showCodeTxt('javascript-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-24">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Accordian.<span style="color: #006600;">prototype</span> = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// do something</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; sectionClicked: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; openSection: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; closeExistingSection: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This object could be instantiated in the following way:</p>
<div class="igBar"><span id="ljavascript-25"><a href="#" onclick="javascript:showCodeTxt('javascript-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-25">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> myAccordian = <span style="color: #003366; font-weight: bold;">new</span> Accordian<span style="color: #66cc66;">&#40;</span>options<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>Explanation: Writing the Constructor</h3>
<p>The constructor needs to take two arguments - the ID of the element that contains the accordian and the type of entry that is used for the header of each section.  The constructors main job is getting a list of all of the sections within the accordian and adds onClick event handlers to the headers so that clicking them will open the section.  The constructor is shown below:</p>
<div class="igBar"><span id="ljavascript-26"><a href="#" onclick="javascript:showCodeTxt('javascript-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-26">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>elem, clickableEntity<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">container</span> = $<span style="color: #66cc66;">&#40;</span>elem<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> headers = $$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#'</span> + elem + <span style="color: #3366CC;">' .section '</span> + clickableEntity<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;headers.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>header<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>header,<span style="color: #3366CC;">'click'</span>,<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">sectionClicked</span>.<span style="color: #006600;">bindAsEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now to explain what's going on in the constructor:</p>
<ul>
<li>The first line defines the function that expects two variables - the ID of the element that holds the accordian and the type of element that will serve as the header for a section.</li>
<li>The second line sets a class property, 'container', to the element passed in with variable 'elem'.  It uses Prototype's $() function, which in this case is equivalent to document.getElementById()</li>
<li>The third line uses the very powerful $$() function that returns an array of all elements that match a given css query.  In this case, the query might look like '#myAccordian .section h3', which would return all h3 elements under an element with class 'section' under the element with ID 'myAccordian'.</li>
<li>Lines 4-6 setup the event handlers that will be used when the user clicks the headers.  This uses the each() function that Prototype adds to arrays in order to quickly loop through an array without using a for loop or creating a bunch of temporary variables.  This function takes a function as an argument that will be applied as an iterator over the array.  This use of this technique is similar to the foreach concept in PHP (foreach($headers as $header)) and other languages.  Because functions are objects in JavaScript, we need to add a trick at the end of the each function defined within 'each' - we need to add 'bind(this)'.  This means that within the new function, the 'this' keyword will be bound to the Accordian object, not to the function itself.</li>
<li>Line 5 uses Prototype's Event class to setup the observer - in this case it will bind the classes sectionClicked method to any click on the header elements. Similar to the binding needed on the function, the reference to the sectionClicked function is follwed by 'bindAsEventListener(this)', which means that inside the event handler, 'this' will refer to the Accordian object, not to the clicked header element.</li>
</ul>
<h3>Explanation: Writing the Event Handler</h3>
<p>The function referenced as the event handler in the constructor is shown below:</p>
<div class="igBar"><span id="ljavascript-27"><a href="#" onclick="javascript:showCodeTxt('javascript-27'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-27">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sectionClicked: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">openSection</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">element</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parentNode</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Functions called as an event always get passed an event object.  This function takes this event and uses the Prototype 'Event' class to get a handle on the parent node of the element that was clicked, the section itself. It then passes this as an argument to another method in this class - openSection.</p>
<h3>Explanation: Opening a Section</h3>
<p>This function if fairly straightfoward - it checks to see if the section being opened is already open, and if not, calls a function to close the open section, and finally opens the new section.  It is shown below:</p>
<div class="igBar"><span id="ljavascript-28"><a href="#" onclick="javascript:showCodeTxt('javascript-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-28">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">openSection: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>section<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> section = $<span style="color: #66cc66;">&#40;</span>section<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>section.<span style="color: #006600;">id</span> != <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">closeExistingSection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span> = section.<span style="color: #006600;">id</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> contents = document.<span style="color: #006600;">getElementsByClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contents'</span>,section<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; contents<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<ul>
<li>The function takes one argument, the section that is to be opened</li>
<li>The second line uses the $() function to get the DOM element.  It is important to note that this function can either take the section ID as an argument or the actual section DOM node.  The $() function adds this extra flexibility</li>
<li>The third line checks to see whether the ID of the section is the same as the currently open section.  If it is, no other logic is performed.  If it isn't, the next few lines will open that section.</li>
<li>After line 4 calls a function to close the existing section, line 5 sets a class property 'currentSection' to the new section</li>
<li>Line 6 uses Prototype's getElementsByClassName, passing the class (contents) and the element under which it should look (the new section).  This returns an array of elements</li>
<li>Line 7 takes the first element with class contents (their should only be one), and uses Prototype's show() function to show the element</li>
</ul>
<h3>Explanation: Closing a Section</h3>
<p>This last function is the simplest of all - it checks to see whether there is an open section, and if so, it hides it.</p>
<div class="igBar"><span id="ljavascript-29"><a href="#" onclick="javascript:showCodeTxt('javascript-29'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-29">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">closeExistingSection: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> contents = document.<span style="color: #006600;">getElementsByClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contents'</span>,<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; contents<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<ul>
<li>The second line checks if the currentSection exists - it is only set after the first section has been opened</li>
<li>The third line uses the same technique as in the previous function - it uses getElementsByClassName to get the elements with class 'section'</li>
<li>Finally, the hide function will hide this element so that the section is collapsed</li>
</ul>
<h3>The Final Script</h3>
<div class="igBar"><span id="ljavascript-30"><a href="#" onclick="javascript:showCodeTxt('javascript-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-30">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Accordian = <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Accordian.<span style="color: #006600;">prototype</span> = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>elem, clickableEntity<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">container</span> = $<span style="color: #66cc66;">&#40;</span>elem<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> headers = $$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#'</span> + elem + <span style="color: #3366CC;">' .section '</span> + clickableEntity<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; headers.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>header<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>header,<span style="color: #3366CC;">'click'</span>,<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">sectionClicked</span>.<span style="color: #006600;">bindAsEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; sectionClicked: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">openSection</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">element</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parentNode</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; openSection: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>section<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> section = $<span style="color: #66cc66;">&#40;</span>section<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>section.<span style="color: #006600;">id</span> != <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">closeExistingSection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span> = section.<span style="color: #006600;">id</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> contents = document.<span style="color: #006600;">getElementsByClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contents'</span>,section<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contents<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; closeExistingSection: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> contents = document.<span style="color: #006600;">getElementsByClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contents'</span>,<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">currentSection</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contents<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>Conclusion</h3>
<p>Though this is a fairly simple example, I hope it helped to explain some of the features of the Prototype library and can get people kickstarted to writing classes with JavaScript.  Most of it is very simple...if only there were some real documentation.  The best source right now is probably <a href="http://www.sergiopereira.com/articles/prototype.js.html">Developer notes for prototype.js</a>, which covers features up to Prototype 1.4.  Another site with a compilation of links is <a href="http://www.prototypedoc.com/">http://www.prototypedoc.com/</a>.</p>
<p>Go forth and rid the world of nasty IE5 style, unreadable, browser-sniffing, hackish, global-scope JavaScript!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2006/08/30/creating-an-accordian-widget-class-with-prototype/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ajax Form Validation</title>
		<link>http://www.gregphoto.net/index.php/2006/07/30/ajax-form-validation/</link>
		<comments>http://www.gregphoto.net/index.php/2006/07/30/ajax-form-validation/#comments</comments>
		<pubDate>Mon, 31 Jul 2006 00:04:58 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/index.php/2006/07/30/ajax-form-validation/</guid>
		<description><![CDATA[Recently I put together a reusable Ajax form validation component that I'd like to share.  It is a JavaScript object that collects the values of all of the elements in a form, sends it for processing to the server, and displays any errors.  If there are no errors it submits the form.  [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I put together a reusable Ajax form validation component that I'd like to share.  It is a JavaScript object that collects the values of all of the elements in a form, sends it for processing to the server, and displays any errors.  If there are no errors it submits the form.  There are several benefits to this approach</p>
<ul>
<li>Allows you to perform complex validation, such as verifying the uniqueness of a username, or checking something in the database...all without reloading the page</li>
<li>Allows you to use the same validation routine for both client-side validation and server-side validation</li>
<li>Reduces the amount of client-side code needed for validation</li>
</ul>
<p>The JavaScript used in <a href="http://www.gregphoto.net/projects/ajaxValidate">my example</a> requires the <a href="http://prototype.conio.net/">Prototype library</a>.  My example uses PHP to perform the validation - more specifically, I've used several components of the <a href="http://framework.zend.com">Zend Framework</a> (PHP5 only).  The example uses:</p>
<ul>
<li>Zend_Controller_* classes including the new RewriteRouter to handle the routing within the application</li>
<li>Zend_View to handle the output</li>
<li>Zend_View_Helper_* classes to build form elements</li>
<li>Zend_Config to read in configuration options</li>
<li>Zend_Filter to perform several field validations</li>
<li>Zend_Json to serialize PHP variables/objects to <a href="http://www.json.org/">JSON objects</a></li>
</ul>
<p>Did I need all of this stuff on the server-side to put together a simple form validation example? Heck no, but when I'm experimenting with new stuff I like to use a lot of new components so I can learn more about them and be prepared for when I actually do need to use all of this fire power.  I've been following the Zend Framework carefully, so I wanted to use some of the stuff I hadn't used yet - particularly the controller architecture.</p>
<h3>The HTML and JavaScript</h3>
<p>Ok, back to how this JavaScript is used.  It's actually really quite simple.  First, you need to include the JavaScript files for both Prototype and <a href="http://www.gregphoto.net/projects/ajaxValidate/static/js/ajaxFormValidate.js">AjaxFormValidator</a> in the head of your HTML document:</p>
<div class="igBar"><span id="lhtml-36"><a href="#" onclick="javascript:showCodeTxt('html-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-36">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"/ajaxValidate/static/js/prototype.js"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"/ajaxValidate/static/js/ajaxFormValidate.js"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Next, you add an 'onSubmit' handler to the form you'd like to validate - in this case, mine looks like this:</p>
<div class="igBar"><span id="lhtml-37"><a href="#" onclick="javascript:showCodeTxt('html-37'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-37">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">&lt;form</span></a> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">"/ajaxValidate/index/success"</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">"POST"</span> <span style="color: #000066;">onSubmit</span>=<span style="color: #ff0000;">"return submitForm(this);"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The form action is set to the page I'd like the script to go to if the validation is successful.  The onSubmit calls a custom function that we still need to define.  It passes 'this' as an argument - the 'this' refers to the form element itself.</p>
<p>Now we define the submitForm() function that the onSubmit of the form will call - it looks like the following:</p>
<div class="igBar"><span id="ljavascript-38"><a href="#" onclick="javascript:showCodeTxt('javascript-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-38">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> submitForm<span style="color: #66cc66;">&#40;</span>form<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; validator = <span style="color: #003366; font-weight: bold;">new</span> AjaxFormValidator<span style="color: #66cc66;">&#40;</span>form,<span style="color: #3366CC;">"/ajaxValidate/index/validate"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; validator.<span style="color: #006600;">errorDisplay</span> = <span style="color: #3366CC;">'inline'</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; validator.<span style="color: #006600;">inlineElem</span> = <span style="color: #3366CC;">'errorDiv'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> validator.<span style="color: #006600;">validate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This function first creates a new AjaxFormValidator object, passing it the form element (or the form id) and the url the validation request should be sent to.  The next two lines setup the type of error display that should happen.  The following error display types are supported:</p>
<ul>
<li>alert: The default option.  Displays all errors in a JavaScript alert box.</li>
<li>inline: Displays errors within an element that is defined on the page.  You need to set the element name with validator.inlineElem.  In this example, errors will be shown in a div with id="errorDiv".</li>
<li>none: errors won't be shown to the user.  The will be available as an array of errors in the errors property of the validator (i.e. validator.errors).  If there are no errors the property will be set to false.  Once you have the errors you can perform more advanced error handling/display to the user.</li>
</ul>
<p>Lastly, validator.validate() will send the values in the form, in an HTTP POST, to the validation url you've provided (/ajaxValidate/index/validate in this case).  If there are no errors the form will be submitted, if there are errors the will be shown (or not shown if you've chosen 'none' as the errorDisplay).</p>
<h3>The PHP</h3>
<p>Once the request is sent it will eventually arrive at the server for processing - in this case, through the magic of routing, it is going to end up in my IndexController in the validateAction method.  This function is shown below:</p>
<div class="igBar"><span id="lphp-39"><a href="#" onclick="javascript:showCodeTxt('php-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-39">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public <span style="color:#000000; font-weight:bold;">function</span> validateAction<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$errors</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">getErrors</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$errors</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0000FF;">$errors</span> = <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> Zend_Json::<span style="color:#006600;">encode</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$errors</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This function calls another method within the IndexController class called getErrors, passing in the entire array of POST arguments (values in the form).  It receives back an array full of error strings.  If there are no errors, it changes the value of the $error variable to false.  Lastly, it uses Zend_Json to convert the PHP array/Boolean into the equivalent JavaScript array/Boolean.  This string is printed and sent back to the client.</p>
<p>The getErrors function itself is very straightforward - it just does a couple validations and returns back an array of the errors.</p>
<div class="igBar"><span id="lphp-40"><a href="#" onclick="javascript:showCodeTxt('php-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-40">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">protected <span style="color:#000000; font-weight:bold;">function</span> getErrors<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$values</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$errors</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>!Zend_Filter::<span style="color:#006600;">isRegex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$values</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'fname'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#FF0000;">'/^[a-z]+[a-z 0-9-_]*$/i'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errors</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">'First Name is required and may only contain letters, numbers, and spaces'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>!Zend_Filter::<span style="color:#006600;">isRegex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$values</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'lname'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#FF0000;">'/^[a-z]+[a-z 0-9-_]*$/i'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errors</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">'Last Name is required and may only contain letters, numbers, and spaces'</span>;</div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>!Zend_Filter::<span style="color:#006600;">isRegex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$values</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'email'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#FF0000;">'/^[_a-zA-Z-]+[._a-zA-Z0-9-]+@[a-zA-Z0-9-]+<span style="color:#000099; font-weight:bold;">\.</span>[.a-zA-Z0-9]+$/'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errors</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">'Email is required and must be a valid email address'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>!Zend_Filter::<span style="color:#006600;">isAlnum</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$values</span><span style="color:#006600; 