<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><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" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Graphics by Greg</title>
	
	<link>http://www.gregphoto.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 27 Jun 2009 22:11:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/GraphicsByGreg" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="graphicsbygreg" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Review: Balsamiq Mockups</title>
		<link>http://www.gregphoto.net/index.php/2009/04/22/review-balsamiq-mockups/</link>
		<comments>http://www.gregphoto.net/index.php/2009/04/22/review-balsamiq-mockups/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 06:51:16 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/?p=59</guid>
		<description><![CDATA[In my job as a product manager one of my key responsibilities is designing high-productivity software that is easy to use and powerful.  We work in a complex domain that involves massive amount of data, millions of dollars on the line, and tight deadlines.  We attack these complex software with advanced technology, masked [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.stratify.com">job</a> as a product manager one of my key responsibilities is designing high-productivity software that is easy to use and powerful.  We work in a complex domain that involves massive amount of data, millions of dollars on the line, and tight deadlines.  We attack these complex software with advanced technology, masked in easy to use software.</p>
<p>One of the challenges with user interface development &#8211; and all software development, for that matter &#8211; is that the further down the line in development something goes, the harder it is to change.  Once a developer spends days or weeks working on something the resistance becomes huge as people are attached to their work &#8211; regardless of whether it is a good fit for the customer.  To counter these problems, I&#8217;m a big advocate of building mockups in order to sort out the key design issues before significant development happens.</p>
<p>Over the years, I&#8217;ve used the following types of tools to do this:</p>
<ul>
<li>Whiteboards with digital camera snapshots</li>
<li>Pen and paper sketches scanned</li>
<li>Photoshop / image mockups</li>
<li>HTML mockups
<li>
<li>PHP prototypes</li>
</ul>
<p>I use these various tools to build mockups for a variety of applications including native windows applications, classical web applications, and dynamic ajax applications.  The more complex applications become, with complex widgets such as data grids, trees, and tabs, the tougher the hurdles are to crank out a quick mockup.  The other problem I&#8217;ve seen is that the closer to &#8216;real&#8217; a mockup looks, the more people are going to comment on the nitty gritty details instead of focusing on the key issues of workflow and usability &#8211; just like the problem of developers getting attached to something they&#8217;ve worked on, reviewers of super realistic models seem to often just assume that they can&#8217;t question the general structure and end up commenting on alignment issues, colors, etc.</p>
<p>I&#8217;m always on the prowl for new tools to use in this design phase, and have recently come across a product that is becoming a great addition to my arsenal of tools: <a href="http://www.balsamiq.com/products/mockups">Balsamiq Mockups</a>.  Balsamiq is a lightweight tool for building wireframes/mockups that look like sketches.  The focus is on rapid creation of screen mockups that are easy to make and easy to change, encouraging feedback and rapid iteration.  The product is available in a desktop version and also version integrated with Wiki software like Confluence.  For the past few weeks I&#8217;ve been playing around with the desktop version &#8211; their website said that they&#8217;d give a free license of the desktop version if you promised to write a review, so here I am! Here I&#8217;d like to point out some of my likes, some annoyances, some features I&#8217;d like to see, and a weird little bug I came across.</p>
<p>First off, here&#8217;s a sample of what a mockup looks like for a fictional, non-sense product:</p>
<p><a href="http://www.gregphoto.net/wp-content/uploads/2009/04/sample.png"><img src="http://www.gregphoto.net/wp-content/uploads/2009/04/sample-300x233.png" alt="Balsamiq Mockup screenshot" title="Balsamiq Mockup screenshot" width="300" height="233" class="aligncenter size-medium wp-image-61" /></a></p>
<p>In a way, Balsamiq seems to be very much inspired by Microsoft PowerPoint&#8230;but in a good way.  You drag shapes from a list of about 75 widgets onto a page and then rearrange, align, and change properties for the items.  The big difference is in the different types of widgets that are available.  There are some basic shapes, but most of the widgets are things you would see in applications, like text inputs, dropdowns, radio/checkboxes, and then some more complex widgets like tabs, trees, data grids, and iPhone widgets.  The result is that you can build things <u>fast</u> &#8211; the mockup above took about 5 minutes to put together.</p>
<h4>What I like</h4>
<ul>
<li>Easy to use &#8211; the number one feature.  As mentioned, the application uses many known paradigms from existing applications such as PowerPoint and other design apps.  I was able to get going without reading the manual, going through a tutorial, or getting a lesson &#8211; the application explains itself.</li>
<li>Fast &#8211; ok, this is tied for the number one feature.  Because of its ease of use, it is possible to create and iterate on designs very quickly. The &#8216;Quick Add&#8217; feature really speeds things up by letting you type in the name of a widget without having to find it in the mix of 75 widgets &#8211; conveniences like this can make a massive difference in productivity.</li>
<li>Just the right level of realism &#8211; the mockups definitely would never be confused with a real application, but they are clean enough that they can convey to point very well and get people to focus on the key design and workflow issues early on in product design and development</li>
<li>Advanced widgets such as tree and data table &#8211; these advanced widgets are some of the items that make it tough to build mockups for advanced apps in languages like HTML without investing a lot of effort to use libraries like extjs, jquery, or YUI.</li>
<li>Easy to export to PNG &#8211; mockups can easily be exported out to the PNG format for posting on a wiki or emailing.</li>
<li>Editable &#8211; as compared to paper mockups, these have the major advantage of being editable, so you don&#8217;t have to break out the eraser or start over from scratch.</li>
<li>Translates well to traditional webapps, ajax webapps, and desktop applications &#8211; because the application</li>
</ul>
<h4>Annoyances&#8230;or things that aren&#8217;t necessarily bugs, but would make the application easier to use if they were addressed</h4>
<ul>
<li>After &#8216;Quick Add&#8217; clicking f2 doesn&#8217;t go into edit mode &#8211; you have to click the widget first.  If you drag items from the strip they automatically go into edit mode, it would be nice to have that for the quick add as well.</li>
<li>Dragging items from the strip doesn&#8217;t show the guides unless you place the widget then drag again, whereas normally when you are dragging you see guides to help you line up widgets</li>
<li>It would be nice if there were a better way to select a set of items without moving the item beneath it.  If you have 20 widgets on a dialog box and you need to shift them over to make space, in order to select them all you have to first lock the dialog box, select the items by click dragging, drag, release, and unlock the dialog.  If you don&#8217;t do this then the dialog box will just be moved.  This gets more confusing if you have multiple boxes on top of each other.  It would be nice if there were a way to move into selection mode which would allow you to select any elements that are entirely enclosed by a bounding box.</li>
<li>Related to the previous entry, you unlock items through a right-click menu (the only usage of right-click in the app?), but if multiple items are locked in the clicked area it just shows the item type in the context menu.  In some scenarios I had 3 or 4 items showing up, some of which were of the same type.  It would be nice to highlight the item when hovering over it in the context menu to let you know you&#8217;ve got the right one.</li>
<li>It would be nice if the tree could user smaller fonts &#8211; some of the other widgets allow for choosing smaller fonts to fit more text, but the tree uses a very large font, making it tough to represent even remotely realistic data.</li>
</ul>
<h4>Features I&#8217;d like to see</h4>
<ul>
<li>Ability to click through from one mockup to another to create workflow &#8211; while I don&#8217;t need to create an entire flow for an application, if I&#8217;m mocking up a particular feature it would be useful to show 3 or 4 screens that are linked together maybe with a generated HTML files or something of the sort.</li>
<li>Ability to regroup after ungrouping items &#8211; you can group items, but you need to ungroup to make any edits (unlike PowerPoint which allows you to change text or even move sub-items), but then you don&#8217;t have an option to regroup&#8230;meaning you have to go through the same fun of locking outer elements, and reselecting.</li>
<li>Radio and Check Box controls should allow adding multiple at once similar to Dropdown &#8211; since these fields usually come with multiple entries it would be nice to have the widgets allow for adding multiple &#8216;rows&#8217; of data similar to dropdowns</li>
<li>It would be nice to have right-click context menus for some operations like grouping or ungrouping</li>
<li>Layers &#8211; it might be nice to have layers (like Photoshop) to help avoid some of the issues with moving items&#8230;this would add a lot of utility but it would come with the negative impact on ease of use and ramp up time.</li>
<li>Ability to add custom widgets &#8211; it would be nice to be able to add custom widgets&#8230;now if you create a widget by composing the built in widgets you have to save it as another mockup and then select-all and copy/paste into the destination mockup &#8211; it would be nice to be able to save the widget to the strip and give it a name.</li>
<li>Favorites bar in addition to the current &#8216;All&#8217;, &#8216;Big&#8217;, &#8216;Buttons&#8217; &#8211; for many projects only a small number of the widgets are necessary.  In my applications I have no need for things like the iPhone widgets so they just get in the way.</li>
<li>Maybe a way to switch from click mode to select mode for selecting multiple items</li>
</ul>
<h4>Bug</h4>
<ul>
<li>This was a weird one.  If you type &#8217;scro&#8217; into the Quick Add box and click the entry for the &#8216;vertical scrollbar&#8217;, the horizontal scrollbar is actually added.  Surprisingly, if you type in &#8217;scrol&#8217; or &#8217;scr&#8217; it works just fine!</li>
</ul>
<h4>Conclusion</h4>
<p>Overall I really like this application &#8211; I&#8217;ve found myself evangelizing to colleagues and used it for some real feature design and have gotten good feedback from all who have seen the results.  Like all software, there are many things that could be improved and there likely always will be, but it seems that the application is in active development and the focus on usability and user productivity look like they&#8217;ll keep the product on the right trajectory.  At $79 I think it is a bargain and justifies its cost in hours or days of use.  It doesn&#8217;t necessarily replace all of the other tools in my mockup toolbox &#8211; to get to the finer level of detail I still need to break out the HTML occasionally  &#8211; but this is now usually something that happens only after I&#8217;ve validated the basic design with Balsamiq.</p>
<p>Rating: 8/10</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2009/04/22/review-balsamiq-mockups/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Playing mp3s with SoundManager 2</title>
		<link>http://www.gregphoto.net/index.php/2008/11/23/playing-mp3s-with-soundmanager-2/</link>
		<comments>http://www.gregphoto.net/index.php/2008/11/23/playing-mp3s-with-soundmanager-2/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 01:10:32 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.gregphoto.net/?p=56</guid>
		<description><![CDATA[My friend Michael Ross is a musician and he sent me an email with a couple of the songs he recorded recently.  I asked him if he would like me to put together a simple web page for his songs so that he didn&#8217;t have to send out emails with large attachments, and a [...]]]></description>
			<content:encoded><![CDATA[<p>My friend Michael Ross is a musician and he sent me an email with a couple of the songs he recorded recently.  I asked him if he would like me to put together a simple web page for his songs so that he didn&#8217;t have to send out emails with large attachments, and a week or so later we had a new site up and running for him &#8211; <a href="http://www.michaelrossmusic.net">www.michaelrossmusic.net</a>.  He wanted a page that listed the albums, had links to download mp3 songs, and a way for people to listen to the songs without downloading them.  I had used <a href="http://www.schillmania.com/projects/soundmanager2/">SoundManager 2</a> <a href="http://www.gregandyenari.com/happyholidays">previously</a> to have mp3s play on a page, so I knew that it would be a good fit.</p>
<p>I wanted to be able to build a simple page with regular links to the mp3 files and for JavaScript to do the magic of making the songs playable in the browser &#8211; this post explains how I made it happen.  SoundManager 2 is a JavaScript library built on top of Flash &#8211; the music plays through Flash (which has much better audio support than browsers), but everything is controlled via JavaScript.  I also used <a href="http://www.prototypejs.org">Prototype</a> as a framework to build out the functionality.</p>
<p>Here&#8217;s what I did:</p>
<ol>
<li>Created a <a href="http://www.michaelrossmusic.net">simple page</a> with links to mp3 files</li>
<li>Added a SoundManager handler so that as soon as it loads it instantiates PagePlayer, a JavaScript class I created</li>
<li>PagePlayer takes a CSS selector as input (and optionally an options object) and identifies all of the links that match the selector &#8211; &#8216;a.mp3Link&#8217; in this case (all links with the class &#8216;mp3Link&#8217;).</li>
<li>For each of the identified links, PagePlayer creates a SoundManager sound object using the link to the mp3, adds a play button just prior to the song link, creates event handlers for the play button and sound object, and stores the song in a hash</li>
<li>The event handlers take care of all of the logic: playing songs, making sure that only a single song is playing, changing the state of the button from play to stop</li>
</ol>
<p>Here&#8217;s a current snapshot of the script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">PagePlayer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	songCount<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
	songs<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">new</span> Hash<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	nowPlaying<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
	options<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
	initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>selector<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
			imgPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">'images/'</span><span style="color: #339933;">,</span>
			imgPlay<span style="color: #339933;">:</span> <span style="color: #3366CC;">'control_play.png'</span><span style="color: #339933;">,</span>
			imgStop<span style="color: #339933;">:</span> <span style="color: #3366CC;">'control_stop.png'</span><span style="color: #339933;">,</span>
			mp3LinkTitle<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Download song'</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		Object.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span><span style="color: #339933;">,</span> options <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$$<span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addSong</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	addSong<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>mp3Link<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		mp3Link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">mp3LinkTitle</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> songId <span style="color: #339933;">=</span> mp3Link.<span style="color: #660066;">identify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> soundObj <span style="color: #339933;">=</span> soundManager.<span style="color: #660066;">createSound</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			id<span style="color: #339933;">:</span> songId<span style="color: #339933;">,</span>
			url<span style="color: #339933;">:</span> mp3Link.<span style="color: #660066;">href</span><span style="color: #339933;">,</span>
			onfinish<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">finishHandler</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>songId<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> controlBtn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
			src<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPath</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPlay</span><span style="color: #339933;">,</span>
			className<span style="color: #339933;">:</span> <span style="color: #3366CC;">'audioControl'</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		mp3Link.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>before<span style="color: #339933;">:</span> controlBtn<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		controlBtn.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">toggleHandler</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>songId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">songs</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>songId<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
			controlBtn<span style="color: #339933;">:</span> controlBtn<span style="color: #339933;">,</span>
			soundObj<span style="color: #339933;">:</span> soundObj
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">songCount</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	toggleHandler<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>songId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> oldSong <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nowPlaying</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> newSong <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">songs</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>songId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>oldSong<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			oldSong.<span style="color: #660066;">soundObj</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			oldSong.<span style="color: #660066;">controlBtn</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPath</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPlay</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>oldSong <span style="color: #339933;">===</span> newSong<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nowPlaying</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		newSong.<span style="color: #660066;">controlBtn</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPath</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgStop</span><span style="color: #339933;">;</span>
		newSong.<span style="color: #660066;">soundObj</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nowPlaying</span> <span style="color: #339933;">=</span> newSong<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	finishHandler<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nowPlaying</span>.<span style="color: #660066;">controlBtn</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPath</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">imgPlay</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nowPlaying</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And on the page it is started up as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;js/soundmanager2-nodebug-jsmin.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;js/pagePlayer.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
	soundManager.url = '';
	soundManager.onload = function() {
		new PagePlayer('a.mp3Link');
	};
&lt;/script&gt;</pre></td></tr></table></div>

<p>It&#8217;s not the prettiest code, but it gets the job done in 1.58 KB of code and works at least in recent versions of Firefox, IE, Safari, and Chrome.  So, if you need to add some mp3s to a page, check out <a href="http://www.schillmania.com/projects/soundmanager2/">SoundManager 2</a> &#8211; it&#8217;s easy to use, well documented, and super flexible&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2008/11/23/playing-mp3s-with-soundmanager-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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 &#8211; 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>
		<slash:comments>2</slash:comments>
		</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 &#8211; 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 &#8211; 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>
		<slash:comments>2</slash:comments>
		</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&#8217;ve written anything here &#8211; about 9 months!  This time around I&#8217;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&#8217;s more fun to rewrite it, right?
For those who don&#8217;t know about [...]]]></description>
			<content:encoded><![CDATA[<p>Looooong time since I&#8217;ve written anything here &#8211; about 9 months!  This time around I&#8217;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&#8217;s more fun to rewrite it, right?</p>
<p>For those who don&#8217;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 &#8217;spaminess&#8217; 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&#8230;the usage of this class is quite straightforward, as shown below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'library/Gregphoto/Defensio.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'library/Gregphoto/Defensio/Adapter/Streams.php);
&nbsp;
$apiKey = '</span>mysecretapikey<span style="color: #0000ff;">';
$siteUrl = '</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//my.secret.site.url.com';</span>
&nbsp;
<span style="color: #000088;">$defensio</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Gregphoto_Defensio<span style="color: #009900;">&#40;</span><span style="color: #000088;">$apiKey</span><span style="color: #339933;">,</span><span style="color: #000088;">$siteUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$defensio</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Gregphoto_Defensio_Adapter_Streams<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'user-ip'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'article-date'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2007/11/24'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'comment-author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Big Bad Spammer'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'comment-type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'comment'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'comment-content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'please click links and buy viagra'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'comment-author-email'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bigbadspammer@annoying.com'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'comment-author-url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.annoying.com'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$defensio</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">audit_comment</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'spam'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'true'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Comment is spam with a spam score of &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'spaminess'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Comment is not spam&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<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&#8217;s Http Stream Wrappers, but realized that didn&#8217;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&#8217;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&#8217;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 &#8216;validate-key&#8217;) 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>
		<slash:comments>2</slash:comments>
		</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>
		<slash:comments>1</slash:comments>
		</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&#8217;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&#8217;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> &#8211; you specify a maximum height and the dimensions are calculated based off of the height</li>
<li><i>MAX_WIDTH</i> &#8211; you specify a maximum width and the dimensions are calculated based off of the width</li>
<li><i>BEST_FIT</i> &#8211; 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> &#8211; 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 &#8211; 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 &#8211; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/to/Gregphoto_Image.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Gregphoto_Image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/to/sample/image.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMaxHeight</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMaxWidth</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setJpegQuality</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span>Gregphoto_Image<span style="color: #339933;">::</span><span style="color: #004000;">BEST_FIT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">showThumbnail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Gregphoto_Image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../images/fan.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMaxHeight</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setJpegQuality</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">showThumbnail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/to/Gregphoto_Image.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Gregphoto_Image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/to/image.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMaxHeight</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMaxWidth</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setJpegQuality</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOutputType</span><span style="color: #009900;">&#40;</span>IMAGETYPE_PNG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span>Gregphoto_Image<span style="color: #339933;">::</span><span style="color: #004000;">BEST_FIT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveThumbnail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path/to/thumbnail.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></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>
		<slash:comments>40</slash:comments>
		</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&#8217;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&#8217;ve put up, I figured I&#8217;d add another one that covers the process of updating a database with the results of [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;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&#8217;ve put up, I figured I&#8217;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&#8217;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 &#8211; the UI that shows the list and has the JavaScript</li>
<li>ajax.php &#8211; the page that processes the Ajax request</li>
<li>db.php &#8211; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$demo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SortableExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$list</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$demo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Scriptaculous 1.7 Sortables Ajax Example&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;style.css&quot;&gt;
	&lt;script src=&quot;js/prototype.js&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;js/scriptaculous.js&quot;&gt;&lt;/script&gt;
	&lt;script&gt;
		Event.observe(window,'load',init,false);
		function init() {
			Sortable.create('listContainer',{tag:'div',onUpdate:updateList});
		}
		function updateList(container) {
			var url = 'ajax.php';
			var params = Sortable.serialize(container.id);
			var ajax = new Ajax.Request(url,{
				method: 'post',
				parameters: params,
				onLoading: function(){$('workingMsg').show()},
				onLoaded: function(){$('workingMsg').hide()}
			});
		}
		function handleResponse(req) {
			// this function will fire after the ajax request is complete...but we have nothing to do here
		}
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&nbsp;
&lt;h2&gt;Scriptaculous Sortables Demo with Ajax Callback&lt;/h2&gt;
Built with Scriptaculous 1.7 Beta 2.  Drag items in the list below.  Each time you update the list an Ajax call is made
that updated the database with the new order.&lt;br&gt;&lt;br&gt;
&nbsp;
&lt;div id=&quot;listContainer&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$list</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;div id=&quot;item_<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'catid'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&lt;div id=&quot;workingMsg&quot; style=&quot;display:none;&quot;&gt;Updating database...&lt;/div&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Let&#8217;s look at this in reverse order.  At the bottom of the file there is a div with id &#8216;containerDiv&#8217; (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 &#8216;item_12&#8242; 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&#8217;s class to get the associative array that is used here.</p>
<p>Underneath the container div is another div that has the text &#8216;Updating database&#8230;&#8217; &#8211; this div is hidden by default and will be shown while Ajax calls are in progress (line 45).</p>
<p>Now to the JavaScript&#8230;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 &#8211; 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 &#8216;params&#8217;.  Next, on line 21, an Ajax request is opened with Prototype&#8217;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 &#8216;Updating database&#8230;&#8217; div.  Normally in an Ajax request I&#8217;d also add a callback for onComplete to handle the response from the server, but in this case the Ajax response doesn&#8217;t really matter to me because it is really only a one way communication in this demo &#8211; in a real application I&#8217;d have more error handling and would need to handle error conditions.</p>
<p>That&#8217;s about it for the index page&#8230;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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$demo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SortableExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$demo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">updateList</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'listContainer'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Yup, that&#8217;s it.  Basically it starts off by calling session_start() &#8211; 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&#8217;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&#8217;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 &#8216;real&#8217; application I would definitely have a much cleaner implementation of the database access and would use other DB libraries to connect to the db &#8211; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000000; font-weight: bold;">class</span> SortableExample <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$conn</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$dbname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'scriptaculous'</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbname</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM categories ORDER BY orderid&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$recordSet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recordSet</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$results</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> updateList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$orderArray</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$orderid</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$orderArray</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$catid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$catid</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$catid</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE categories SET orderid=<span style="color: #006699; font-weight: bold;">{$orderid}</span> WHERE catid=<span style="color: #006699; font-weight: bold;">{$catid}</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$recordSet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$orderid</span><span style="color: #339933;">++;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Important note &#8211; this file uses PHP 5 syntax in this object&#8230;you&#8217;d need some changes to get this to run in PHP 4.  I&#8217;ll skip talking about the constructor (which connects to the database) and the getList function (which&#8230;gets the list) in order to focus on the last method, &#8216;updateList&#8217;.  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 &#8216;ORDER BY orderid&#8217; clause is used the list will show in the correct order.</p>
<h3>Summary</h3>
<p>That&#8217;s it&#8230;hope you enjoyed it.  The hopeful moral of the story is&#8230;Ajax is easy, and so is drag and drop&#8230;.especially if you&#8217;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&#8217;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 &#8211; it worked really well.  <a href="http://www.jackslocum.com">Jack Slocum&#8217;s YUI extension</a> is incredibly well done too&#8230;though it&#8217;s a heavy one in terms of file size!  In the end, it comes down to finding the right tool for the job&#8230;</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&#8230;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</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 &#8216;accordian&#8217; &#8211; 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 &#8216;accordian&#8217; &#8211; 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&#8217;d like to walk through it to explain how it works and how to write something like this with Prototype.  For all of Prototype&#8217;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&#8217;ll skip with going through the HTML &#8211; you can see that on the example page if you&#8217;re interested &#8211; it would be helpful in understanding the javaScript here.  In this example I&#8217;ve used version 1.5.0_rc0 of Prototype.  The first line of the accordian.js file has the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Accordian <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Prototype&#8217;s Class.create() sets up Accordian as an object that can be instantiated.  When the object is instantiated the &#8216;initialize&#8217; method, its constructor, will be automatically called.  You setup all of the methods within the object by defining its prototype &#8211; an object with properties and methods.  This could be done in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Accordian.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// do something</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	sectionClicked<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	openSection<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	closeExistingSection<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This object could be instantiated in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myAccordian <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Accordian<span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Explanation: Writing the Constructor</h3>
<p>The constructor needs to take two arguments &#8211; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> clickableEntity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">container</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> headers <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> elem <span style="color: #339933;">+</span> <span style="color: #3366CC;">' .section '</span> <span style="color: #339933;">+</span> clickableEntity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   headers.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>header<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>header<span style="color: #339933;">,</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sectionClicked</span>.<span style="color: #660066;">bindAsEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now to explain what&#8217;s going on in the constructor:</p>
<ul>
<li>The first line defines the function that expects two variables &#8211; 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, &#8216;container&#8217;, to the element passed in with variable &#8216;elem&#8217;.  It uses Prototype&#8217;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 &#8216;#myAccordian .section h3&#8242;, which would return all h3 elements under an element with class &#8217;section&#8217; under the element with ID &#8216;myAccordian&#8217;.</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 &#8216;each&#8217; &#8211; we need to add &#8216;bind(this)&#8217;.  This means that within the new function, the &#8216;this&#8217; keyword will be bound to the Accordian object, not to the function itself.</li>
<li>Line 5 uses Prototype&#8217;s Event class to setup the observer &#8211; 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 &#8216;bindAsEventListener(this)&#8217;, which means that inside the event handler, &#8216;this&#8217; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  sectionClicked<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">openSection</span><span style="color: #009900;">&#40;</span>Event.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parentNode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Functions called as an event always get passed an event object.  This function takes this event and uses the Prototype &#8216;Event&#8217; 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 &#8211; openSection.</p>
<h3>Explanation: Opening a Section</h3>
<p>This function if fairly straightfoward &#8211; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">openSection<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>section<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> section <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>section<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>section.<span style="color: #660066;">id</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">closeExistingSection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span> <span style="color: #339933;">=</span> section.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> contents <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contents'</span><span style="color: #339933;">,</span>section<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		contents<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<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&#8217;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 &#8216;currentSection&#8217; to the new section</li>
<li>Line 6 uses Prototype&#8217;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&#8217;s show() function to show the element</li>
</ul>
<h3>Explanation: Closing a Section</h3>
<p>This last function is the simplest of all &#8211; it checks to see whether there is an open section, and if so, it hides it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">closeExistingSection<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> contents <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contents'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		contents<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<ul>
<li>The second line checks if the currentSection exists &#8211; 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 &#8211; it uses getElementsByClassName to get the elements with class &#8217;section&#8217;</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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Accordian <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Accordian.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> clickableEntity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">container</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> headers <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> elem <span style="color: #339933;">+</span> <span style="color: #3366CC;">' .section '</span> <span style="color: #339933;">+</span> clickableEntity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		headers.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>header<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>header<span style="color: #339933;">,</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sectionClicked</span>.<span style="color: #660066;">bindAsEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	sectionClicked<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">openSection</span><span style="color: #009900;">&#40;</span>Event.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parentNode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	openSection<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>section<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> section <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>section<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>section.<span style="color: #660066;">id</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">closeExistingSection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span> <span style="color: #339933;">=</span> section.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> contents <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contents'</span><span style="color: #339933;">,</span>section<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			contents<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	closeExistingSection<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> contents <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contents'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentSection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			contents<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<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&#8230;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>
		<slash:comments>14</slash:comments>
		</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&#8217;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&#8217;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&#8230;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 &#8211; more specifically, I&#8217;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&#8217;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&#8217;ve been following the Zend Framework carefully, so I wanted to use some of the stuff I hadn&#8217;t used yet &#8211; particularly the controller architecture.</p>
<h3>The HTML and JavaScript</h3>
<p>Ok, back to how this JavaScript is used.  It&#8217;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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;/ajaxValidate/static/js/prototype.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/ajaxValidate/static/js/ajaxFormValidate.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>Next, you add an &#8216;onSubmit&#8217; handler to the form you&#8217;d like to validate &#8211; in this case, mine looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;/ajaxValidate/index/success&quot; method=&quot;POST&quot; onSubmit=&quot;return submitForm(this);&quot;&gt;</pre></td></tr></table></div>

<p>The form action is set to the page I&#8217;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 &#8216;this&#8217; as an argument &#8211; the &#8216;this&#8217; refers to the form element itself.</p>
<p>Now we define the submitForm() function that the onSubmit of the form will call &#8211; it looks like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> submitForm<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	validator <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> AjaxFormValidator<span style="color: #009900;">&#40;</span>form<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;/ajaxValidate/index/validate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	validator.<span style="color: #660066;">errorDisplay</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'inline'</span><span style="color: #339933;">;</span>
	validator.<span style="color: #660066;">inlineElem</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'errorDiv'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> validator.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></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=&#8221;errorDiv&#8221;.</li>
<li>none: errors won&#8217;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&#8217;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&#8217;ve chosen &#8216;none&#8217; as the errorDisplay).</p>
<h3>The PHP</h3>
<p>Once the request is sent it will eventually arrive at the server for processing &#8211; 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="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> validateAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getErrors</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> Zend_Json<span style="color: #339933;">::</span><span style="color: #004000;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>php<span style="color: #339933;">&gt;</span>
This <span style="color: #000000; font-weight: bold;">function</span> calls another method within the IndexController <span style="color: #000000; font-weight: bold;">class</span> called getErrors<span style="color: #339933;">,</span> passing in the entire <span style="color: #990000;">array</span> of POST arguments <span style="color: #009900;">&#40;</span>values in the form<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>  It receives back an <span style="color: #990000;">array</span> full of error strings<span style="color: #339933;">.</span>  <span style="color: #b1b100;">If</span> there are no errors<span style="color: #339933;">,</span> it changes the value of the <span style="color: #000088;">$error</span> variable to <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">.</span>  Lastly<span style="color: #339933;">,</span> it uses Zend_Json to convert the PHP <span style="color: #990000;">array</span><span style="color: #339933;">/</span>Boolean into the equivalent JavaScript <span style="color: #990000;">array</span><span style="color: #339933;">/</span>Boolean<span style="color: #339933;">.</span>  This string is printed and sent back to the client<span style="color: #339933;">.</span>
&nbsp;
The getErrors <span style="color: #000000; font-weight: bold;">function</span> itself is very straightforward <span style="color: #339933;">-</span> it just does a couple validations and returns back an <span style="color: #990000;">array</span> of the errors<span style="color: #339933;">.</span>
<span style="color: #339933;">&lt;</span>pre lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;php&quot;</span> line<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
protected <span style="color: #000000; font-weight: bold;">function</span> getErrors<span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Zend_Filter<span style="color: #339933;">::</span><span style="color: #004000;">isRegex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/^[a-z]+[a-z 0-9-_]*$/i'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'First Name is required and may only contain letters, numbers, and spaces'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Zend_Filter<span style="color: #339933;">::</span><span style="color: #004000;">isRegex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/^[a-z]+[a-z 0-9-_]*$/i'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Last Name is required and may only contain letters, numbers, and spaces'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Zend_Filter<span style="color: #339933;">::</span><span style="color: #004000;">isRegex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/^[_a-zA-Z-]+[._a-zA-Z0-9-]+@[a-zA-Z0-9-]+\.[.a-zA-Z0-9]+$/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Email is required and must be a valid email address'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Zend_Filter<span style="color: #339933;">::</span><span style="color: #004000;">isAlnum</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Password must consist of only letters and numbers and must be between 6 and 12 characters'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$errors</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.gregphoto.net/projects/ajaxValidate">Check it out, in action</a>.  And that&#8217;s about it &#8211; pretty simple, huh?</p>
<p>You can download:</p>
<ul>
<li><a href="http://www.gregphoto.net/projects/ajaxValidate/static/js/ajaxFormValidate.js">The JavaScript</a></li>
<li><a href="http://www.gregphoto.net/projects/ajaxValidate/ajaxFormValidate.zip">The whole PHP project</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.gregphoto.net/index.php/2006/07/30/ajax-form-validation/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
