<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Three till Seven &#187; Javascript</title>
	<atom:link href="http://www.3till7.net/category/techy/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3till7.net</link>
	<description>A geek's personal domain.</description>
	<pubDate>Wed, 27 Aug 2008 22:19:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>regular Javascript v. AJAX for dynamic content insertion</title>
		<link>http://www.3till7.net/2008/02/27/regular-javascript-v-ajax-for-dynamic-content-insertion/</link>
		<comments>http://www.3till7.net/2008/02/27/regular-javascript-v-ajax-for-dynamic-content-insertion/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 22:41:25 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Web development]]></category>

		<category><![CDATA[ajax]]></category>

		<category><![CDATA[list]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2008/02/27/javascript-v-ajax-for-dynamic-content-insertion/</guid>
		<description><![CDATA[Recently for work, I&#8217;ve had cause to write functionality so that a certain chunk of a form can be inserted again, over and over, by the user.  The form involves data about course equivalencies between schools, and the user might need to submit data for multiple courses a student has taken.  Hence, the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently for work, I&#8217;ve had cause to write functionality so that a certain chunk of a form can be inserted again, over and over, by the user.  The form involves data about course equivalencies between schools, and the user might need to submit data for multiple courses a student has taken.  Hence, the user needs to be able to add extra course sections on the fly, as they&#8217;re necessary.  Here&#8217;s a screenshot to better illustrate what I mean:</p>
<div class="photo" style="width:200px;"><a href="/wp-images/2008-02-27_scr.jpg?PHPSESSID=3a61a54546ee031cac1cb118b3d174e7"><img src="/wp-images/2008-02-27_scr.jpg" alt="2008-02-27 screenshot of form fields" width="200" /><br />
View full size &raquo;</a></div>
<p>I decided to do this with Javascript, but the question was:</p>
<ul>
<li>Should I dynamically generate the extra form fields with Javascript before stuffing them into the <a href="http://en.wikipedia.org/wiki/Document_Object_Model"><abbr title="Document Object Model">DOM</abbr></a>,</li>
</ul>
<p><strong>or</strong></p>
<ul>
<li>Should I have a separate page, containing the form fields written in plain HTML, that I dynamically insert into the form page via an <a href="http://en.wikipedia.org/wiki/AJAX"><abbr title="Asynchronous JavaScript and XML">AJAX</abbr></a> call?</li>
</ul>
<p>Contemplating the idea, I decided the best route would be to use AJAX.  Here are some guidelines for making such a choice, based on the logic I used:</p>
<dl class="padded">
<dt>Are you generating a lot of code?</dt>
<dd>If you&#8217;re going to be generating a lot of code each time, like in my example, the AJAX route makes more sense because you can have a separate page (PHP for me, but you could use a straight HTML page) to write that content in HTML, then use an AJAX call to include that page in your main page.  See, if you were to use Javascript for dynamically creating elements (<code>document.createElement()</code> and <code><em>element</em>.appendChild()</code> are your friends here), you&#8217;re going to write a lot more code in Javascript than the straight HTML it takes to create various elements and their attributes.  In my case, with Javascript I would be generating <code>li</code>&#8217;s, <code>span</code>&#8217;s, <code>fieldset</code>&#8217;s, <code>input</code>&#8217;s, and <code>legend</code>&#8217;s, then several of those had the attributes <code>rel</code> and <code>class</code>, not to mention the normal attributes associated with <code>input</code>.  It&#8217;s simpler to write out the HTML for this in a separate page, rather than have lots of Javascript functions for creating those elements, applying attributes to them, assembling them together, then stuffing the result somewhere in the existing page.</dd>
<dt>Does the dynamically added code replicate content already in the main page?</dt>
<dd>In my example, the main form page started out with one course section already showing up.  If I were to use straight Javascript DOM manipulation to allow the user to insert extra course sections, I would be replicating the same content in two ways:  in the main HTML page, and in the Javascript necessary to generate a new course section.  Being a <a href="http://www.ruby-lang.org/en/">Ruby</a> girl at heart and a big supporter of the DRY principle (<strong>D</strong>on&#8217;t <strong>R</strong>epeat <strong>Y</strong>ourself), it hurts something deep inside of me to duplicate that much functionality in my application.  Well, maybe I&#8217;m exaggerating a bit, but I gotta stress this, because such replication leads to harder-to-maintain code.  Any time you want to change the teensiest thing, you have two places in which you need to change it:  the original HTML, and the Javascript that creates the same chunk of HTML before inserting it into your page.  Going the AJAX way here would be best.</dd>
<dt>Are you adding only a small piece of HTML?</dt>
<dd>If you want the user to be able to dynamically add a single link or something equally trivial to your existing page, the AJAX route might be overkill.  The Javascript necessary to generate one or a couple HTML elements isn&#8217;t overly complex or lengthy, and so it makes sense to use Javascript element generation followed by DOM manipulation to add your new content.  I think it would be more trouble than it&#8217;s worth to have a separate page containing just a link or two, and then use AJAX calls to stuff that snippet page into your main page.</dd>
<dt>If your dynamic content isn&#8217;t complex now, do you foresee it becoming so in the future?</dt>
<dd>Maybe for the time being, the user only needs to insert a couple of elements into the page at his or her whim, but in the future, you think your application will expand such that large chunks of HTML will need to be added at a time.  In this case, you might start off with the AJAX route just for your future convenience (or the convenience of another developer&mdash;be considerate of the next guy, right?).  It&#8217;ll make your page easier to expand in the future, since you won&#8217;t have to remove all the Javascript content generation code, create a separate page to hold the content to be dynamically added, then write the AJAX calls to include that content as the user desires.</dd>
<dt>Are you using a PHP (or similar server-side language) back end?</dt>
<dd>The site I was creating my example form for uses PHP, so the AJAX route especially makes sense here, since I can make the dynamic addition of content be available for the user even if they have Javascript disabled.  I&#8217;ll just use PHP to parse a POST request and decide how many course sections to include (using a loop and PHP&#8217;s <code>include</code> on the separate snippet page of content), based on how many they currently have and how many extra they want to add.  With the Javascript content generation route, this sort of thing is impossible, because Javascript is entirely client-side, and if the user doesn&#8217;t have it enabled or their browser doesn&#8217;t support it at all, they&#8217;re up a creek.  Oh sure, you could have the non-AJAX version for those with Javascript, then write functionality in a server-side language to allow the user the same functionality without Javascript enabled, but there you go again with the replication of functionality&mdash;bad programmer!  If you&#8217;re stuck with just HTML, though&mdash;which I have to guess is unlikely in this day and age, what with every Tom, Dick, and Harry of hosts supporting PHP, Perl, <a href="http://en.wikipedia.org/wiki/JavaServer_Pages"><abbr title="JavaServer Pages">JSP</abbr></a>, and Ruby&mdash;you&#8217;ll need to determine whether to go AJAX or Javascript-content-generation based on the other questions raised above.</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2008/02/27/regular-javascript-v-ajax-for-dynamic-content-insertion/feed/</wfw:commentRss>
		</item>
		<item>
		<title>using Javascript and CSS to mark outgoing links</title>
		<link>http://www.3till7.net/2007/09/26/using-javascript-and-css-to-mark-outgoing-links/</link>
		<comments>http://www.3till7.net/2007/09/26/using-javascript-and-css-to-mark-outgoing-links/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 19:11:43 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Web development]]></category>

		<category><![CDATA[css]]></category>

		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2007/09/26/using-javascript-and-css-to-mark-outgoing-links/</guid>
		<description><![CDATA[It may be useful to your visitors to know which links will take them off of your site without having to hover over all of them.  It might also be a nice touch to mark links pointing to PDF documents, for example, with a little Adobe PDF logo.  With the help of Javascript [...]]]></description>
			<content:encoded><![CDATA[<p>It may be useful to your visitors to know which links will take them off of your site without having to hover over all of them.  It might also be a nice touch to mark links pointing to PDF documents, for example, with a little Adobe PDF logo.  With the help of Javascript and CSS, you can do this pretty simply.</p>
<p>First, we need some Javascript to go through our page and add a CSS class to every link that we want to differentiate.</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7337f5c732">
<h2 class="synthi_header"> JAVASCRIPT</h2>
<div class="javascript" style="font-family: monospace;"><span style="color: #009900; font-style: italic;">// Goes through all links in a page and adds CSS</span><br />
<span style="color: #009900; font-style: italic;">// classes to them to better style outgoing links,</span><br />
<span style="color: #009900; font-style: italic;">// PDF links, etc.</span><br />
<span style="color: #003366; font-weight: bold;">function</span> link_classifier<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">// Get an array of all links in the page</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> links = document.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'a'</span> <span style="color: #66cc66;">&#41;</span></p>
<p>&nbsp; <span style="color: #009900; font-style: italic;">// These variables will be used later to store</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">// the current 'a' object we're working with,</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">// and its URI</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> curr_link, uri</p>
<p>&nbsp; <span style="color: #009900; font-style: italic;">// A regular expression to match URI's ending</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">// in 'pdf'</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> pdf_regex = <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'pdf$'</span>, <span style="color: #3366CC;">'i'</span> <span style="color: #66cc66;">&#41;</span></p>
<p>&nbsp; <span style="color: #009900; font-style: italic;">// A regular expression to match URI's beginning</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">// with 'http://', which we will assume go to</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">// other sites</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> outside_regex = <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'^http://'</span>, <span style="color: #3366CC;">'i'</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">0</span>; i&lt;links.<span style="color: #006600;">length</span>; i++ <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; curr_link = links<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; uri = curr_link.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'href'</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Add a CSS class to PDF links</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> pdf_regex.<span style="color: #006600;">test</span><span style="color: #66cc66;">&#40;</span> uri <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; add_class<span style="color: #66cc66;">&#40;</span> curr_link, <span style="color: #3366CC;">'pdf'</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Add a CSS class to outside links</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> outside_regex.<span style="color: #006600;">test</span><span style="color: #66cc66;">&#40;</span> uri <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; add_class<span style="color: #66cc66;">&#40;</span> curr_link, <span style="color: #3366CC;">'outside'</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #009900; font-style: italic;">// Given some object and a class name, this</span><br />
<span style="color: #009900; font-style: italic;">// function will append the given class name</span><br />
<span style="color: #009900; font-style: italic;">// to the CSS class of the object</span><br />
<span style="color: #003366; font-weight: bold;">function</span> add_class<span style="color: #66cc66;">&#40;</span> object, class_name <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> old_class = object.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'class'</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> old_class == <span style="color: #3366CC;">''</span> || old_class == <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; object.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'class'</span>, class_name <span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">else</span><br />
&nbsp; &nbsp; object.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'class'</span>, old_class + <span style="color: #3366CC;">' '</span> + class_name <span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #009900; font-style: italic;">// Tell the browser to run our link_classifier</span><br />
<span style="color: #009900; font-style: italic;">// function when the page loads</span><br />
window.<span style="color: #000066;">onload</span> = link_classifier<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</div>
<p>Now we can style the links based on their class names.  You could do whatever you wanted to with them, maybe make all outgoing links bold, whatever.  For me, I like having <a href="http://www.welie.com/patterns/showPattern.php?patternID=outgoing-links">little icons next to outgoing links</a> or other special links.  If it's a PDF document, maybe use an Adobe logo.  If it's an MP3, a little music note.  Get creative!  Anywho, here's some CSS for sticking such little logos in after the text of a link:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b7337f71323">
<h2 class="synthi_header"> CSS</h2>
<div class="css" style="font-family: monospace;">a<span style="color: #6666ff;">.<span style="color: #993333;">outside</span> </span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'outgoing_link.gif'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">center</span>;<br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding-right</span>: 11px;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p>a<span style="color: #6666ff;">.pdf </span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'pdf.gif'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">center</span>;<br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding-right</span>: 14px;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
<p>I set the right padding to be the width of the icon plus 1, so that the icon wouldn't be pressed right against the text.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2007/09/26/using-javascript-and-css-to-mark-outgoing-links/feed/</wfw:commentRss>
		</item>
		<item>
		<title>using AJAX to implement a live search</title>
		<link>http://www.3till7.net/2007/05/14/using-ajax-to-implement-a-live-search/</link>
		<comments>http://www.3till7.net/2007/05/14/using-ajax-to-implement-a-live-search/#comments</comments>
		<pubDate>Mon, 14 May 2007 14:37:29 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[ajax]]></category>

		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2007/05/14/using-ajax-to-implement-a-live-search/</guid>
		<description><![CDATA[I recently implemented a live search on the quotes page, so I figured I'd give a tutorial on how I did it.  A "live" search is like a regular search feature, but if the user has Javascript enabled, the search will run as they type, instead of waiting for them to hit the 'Submit' [...]]]></description>
			<content:encoded><![CDATA[<p>I recently implemented a live search on the <a href="/quotes/?PHPSESSID=3a61a54546ee031cac1cb118b3d174e7">quotes page</a>, so I figured I'd give a tutorial on how I did it.  A "live" search is like a regular search feature, but if the user has Javascript enabled, the search will run as they type, instead of waiting for them to hit the 'Submit' button.  There are a couple of advantages to this, one being that it's a bit quicker than a traditional search because it starts working immediately; another advantage is that it might help you narrow down your search better (e.g. you initially want to search for 'frogs', but you get just 'frog' typed in and see results that interest you that wouldn't have seen with a 'frogs' query).  However, I think the main benefit from a live search is that it's cool.  :)</p>
<h2 class="space_above">Things You'll Need</h2>
<ul class="padded">
<li>PHP for the search backend;</li>
<li>The <a href="http://www.prototypejs.org/">Prototype</a> Javascript framework in order to easily make an AJAX call with the visitor's search query.</li>
</ul>
<h2 class="space_above">Query-parsing Backend</h2>
<p>We should get our search working without any fancy AJAX first to know that it works.  My quotes are stored in a plain-text file and I use PHP to display them.  We want a PHP script that we can pass a query to, either by a GET or a POST request (i.e. through a URL parameter or from a form), and that will then search through our quotes file for the given query.  Here's what I came up with, which I named quotes_search.php:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b733804d12b">
<h2 class="synthi_header"> PHP</h2>
<div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">// Given a Regular Expression and an array,</span><br />
<span style="color: #808080; font-style: italic;">// it will return an array of all keys for which</span><br />
<span style="color: #808080; font-style: italic;">// the regular expression matches the array</span><br />
<span style="color: #808080; font-style: italic;">// value at that key</span><br />
<span style="color: #000000; font-weight: bold;">function</span> array_regex<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$pattern</span>, <span style="color: #0000ff;">$haystack</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">$keys</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #808080; font-style: italic;">// Go through the given array</span><br />
&nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$haystack</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$i</span> =&gt; <span style="color: #0000ff;">$value</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Check to see if the current array value</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// matches the given pattern</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$pattern</span>, <span style="color: #0000ff;">$value</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Use array_unshift instead of array_push</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// so that the latest quote keys are added</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// to the beginning of the array and not the</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// end--want to display the quote search</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// results in reverse order</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_unshift"><span style="color: #000066;">array_unshift</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$keys</span>, <span style="color: #0000ff;">$i</span> <span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$keys</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #808080; font-style: italic;">// Get the query either from a GET or POST request</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'query'</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'query'</span><span style="color: #66cc66;">&#93;</span>;<br />
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'query'</span>, <span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'query'</span><span style="color: #66cc66;">&#93;</span>;</p>
<p><span style="color: #808080; font-style: italic;">// Where the quotes are stored; plain-text file</span><br />
<span style="color: #0000ff;">$data</span> = <span style="color: #ff0000;">'/path/to/quotes/file/quotes.txt'</span>;</p>
<p><span style="color: #808080; font-style: italic;">// Turn that quotes file into an array of lines</span><br />
<span style="color: #0000ff;">$file</span> = <a href="http://www.php.net/file"><span style="color: #000066;">file</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$data</span> <span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #808080; font-style: italic;">// If the query was blank...</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$query</span> == <span style="color: #ff0000;">''</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">// Just display the entire quotes file</span><br />
&nbsp; <span style="color: #0000ff;">$count</span> = <a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$file</span> <span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;ol class=&quot;padded&quot;&gt;'</span>;</p>
<p>&nbsp; <span style="color: #808080; font-style: italic;">// Display the quotes from the most recent to the earliest</span><br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$i</span>=<span style="color: #0000ff;">$count</span><span style="color: #cc66cc;">-1</span>; <span style="color: #0000ff;">$i</span>&gt;=<span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$i</span>-- <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Get rid of carriage return</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// For odd-numbered indices in the quotes array, put a</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// different CSS class on the li tag so that we can style</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// it differently for readability</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$i</span> % <span style="color: #cc66cc;">2</span> == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;li&gt;'</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;li class=&quot;alt&quot;&gt;'</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Place an anchor tag where the name attribute is set</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// to the quote's index in the quotes array, so that we</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// can link directly to this quote later if we like</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;a name=&quot;'</span> . <span style="color: #0000ff;">$i</span> . <span style="color: #ff0000;">'&quot;&gt;&lt;/a&gt;'</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Display the quote itself</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;/li&gt;'</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;/ol&gt;'</span>;<br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">// Remove any slashes in the query so as not to mess with PHP's</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">// RegEx support</span><br />
&nbsp; <span style="color: #0000ff;">$query</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'/'</span>, <span style="color: #ff0000;">''</span>, <span style="color: #0000ff;">$query</span> <span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #808080; font-style: italic;">// Find the indices that have matching values to the given query,</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">// case insensitive</span><br />
&nbsp; <span style="color: #0000ff;">$keys</span> = array_regex<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;/$query/i&quot;</span>, <span style="color: #0000ff;">$file</span> <span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #808080; font-style: italic;">// If there were any indices found with matching values...</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$keys</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;ol class=&quot;padded&quot;&gt;'</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Go through the indices</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$keys</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$count</span> =&gt; <span style="color: #0000ff;">$key</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// For odd-numbered indices in the quotes array, put a</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// different CSS class on the li tag so that we can style</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// it differently for readability</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$count</span> % <span style="color: #cc66cc;">2</span> == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;li&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;li class=&quot;alt&quot;&gt;'</span>;</p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Display the quote itself</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;/ol&gt;'</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// No matching quotes were found</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;p&gt;No quotes match your search.&lt;/p&gt;'</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div>
</div>
<h2 class="space_above">Displaying the Quotes</h2>
<p>When no query is submitted, we want to see all the quotes.  In my Wordpress Quotes page, I have the following:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73380a0858">
<h2 class="synthi_header"> HTML</h2>
<div class="html" style="font-family: monospace;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;quotes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;</span></a>?php include <span style="color: #ff0000;">'/path/to/searching/backend/quotes_search.php'</span>; ?<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</div>
<p>Since we're including the search backend without any query passed to it, the PHP code shown above will just spit out the quotes in a nice ordered list.  The <code>div</code> tag with its ID "quotes" is very important, as this will be used later by some Javascript to display the search results.</p>
<h2 class="space_above">Making It Live</h2>
<p>Now that we have our PHP backend, we can work on the Javascript side of things.  We still want people with Javascript disabled to be able to use our search, though, so we set up the HTML form like any other form.  We can, however, hide the submit button from those that have Javascript enabled, so as to make it clearer that submitting the form in the usual way is unnecessary.  Here's the HTML I used:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73380a6f1e">
<h2 class="synthi_header"> HTML</h2>
<div class="html" style="font-family: monospace;"><span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">&lt;form</span></a> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;/wp-content/quotes_search.php&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;query&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;query&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;loading&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/em.html"><span style="color: #000000; font-weight: bold;">&lt;em&gt;</span></a></span>Searching quotes...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/em&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;complete&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/em.html"><span style="color: #000000; font-weight: bold;">&lt;em&gt;</span></a></span>Search complete!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/em&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
&nbsp; document.write('<span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/em.html"><span style="color: #000000; font-weight: bold;">&lt;em&gt;</span></a></span>Hint:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/em&gt;</span></span> It searches as you type.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span>');<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/noscript.html"><span style="color: #000000; font-weight: bold;">&lt;noscript&gt;</span></a></span><br />
&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Search&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/noscript&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form&gt;</span></span></div>
</div>
<p>See the <code>span</code> tags that are set to <code>display: none</code>?  Those will be used to notify the visitor that a search is taking place or has completed.  They start out hidden because nothing initially happens with the search; we'll use Javascript to display them and re-hide them as necessary.  In the <code>noscript</code> area, which will be seen by users without Javascript, we include a submit button.  For those with Javascript, we offer a helpful hint that no form submission is necessary.</p>
<p>This HTML by itself won't do anything special.  What we really need is the Javascript code to check when our search input field (with ID "query" above) has been updated and then initialize a search.  Here's that code:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b73380cc921">
<h2 class="synthi_header"> JAVASCRIPT</h2>
<div class="javascript" style="font-family: monospace;">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;<br />
&nbsp; <span style="color: #009900; font-style: italic;">//&lt;![CDATA[</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">new</span> Form.<span style="color: #006600;">Element</span>.<span style="color: #006600;">Observer</span><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">'query'</span>,<br />
&nbsp; &nbsp; <span style="color: #CC0000;">0.5</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>element, value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Updater</span><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'quotes'</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'/url/to/search/backend/quotes_search.php?query='</span> + value,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asynchronous:<span style="color: #003366; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evalScripts:<span style="color: #003366; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>Element.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'loading'</span><span style="color: #66cc66;">&#41;</span>; Element.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'complete'</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onLoading:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>Element.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'complete'</span><span style="color: #66cc66;">&#41;</span>; Element.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'loading'</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span>parameters:value<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #009900; font-style: italic;">//]]&gt;</span><br />
&lt;/script&gt;</div>
</div>
<p>That's a hairy chunk of Javascript, I know.  What it's doing is creating an <a href="http://prototypejs.org/api/timedObserver/form-element-observer">Observer</a> (thank you, Prototype) for the element with the ID "query" (which is our search input field).  The Observer checks the search field every 500 milliseconds for changes.  When changes are detected, it executes the function that is the third parameter.  The function contains an Updater, which will update the element with the ID "quotes", which is our <code>div</code> container holding all the quotes in our page.  The Updater uses AJAX to send a request to the URL <code>'/url/to/search/backend/quotes_search.php?query=' + value</code>, where <em>value</em> will be replaced with the value Javascript extracts from the search input field.  The next Updater parameter is a hash of options, including two for showing and hiding our <code>span</code> tags when a query has completed or is in process.  The final Updater parameter defines what parameters we want from the search input field, for use in our backend.  You'll want to stick this chunk of Javascript somewhere <em>after</em> your <code>div</code> containing the quotes and the search form, so that all the necessary ID's the Javascript uses will be available to it.</p>
<h2 class="space_above">Overall View</h2>
<p>To sum up, here's what we've got and what we're doing with it:</p>
<ol class="padded">
<li>We have a plain-text file containing quotations, called quotes.txt;</li>
<li>We have a Wordpress page in which we want to show these quotes, as well as offer a live search so that users can find specific quotes;</li>
<li>We have a PHP backend in quotes_search.php that will search quotes.txt for a given query;</li>
<li>We display the quotes in our Wordpress page and stick in the search form anywhere we please; after both of these items, we stick in the Javascript that will make our AJAX call;</li>
<li>When a user with Javascript enabled types something in the search field, a Javascript Observer sees this and pokes the Javascript Updater;</li>
<li>The Updater then gets off its butt and grabs the value from the search field, then throws it at quotes_search.php via GET request (that is, it passes the query as a URL parameter);</li>
<li>If the user does not have Javascript enabled, they hit the submit button just like in any other form and the query is passed to quotes_search.php via a POST request (that is, it passes the query as a parameter in the form submission);</li>
<li>The PHP backend takes the query and searches all the quotes in quotes.txt for all quotes that match; it then displays matching quotes in an ordered list.  If Javascript is enabled, the matching quotes will be displayed inside the "quotes" <code>div</code>; if Javascript is disabled, the quotes will be displayed in a standalone page.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2007/05/14/using-ajax-to-implement-a-live-search/feed/</wfw:commentRss>
		</item>
		<item>
		<title>using AJAX to spiff up a gallery</title>
		<link>http://www.3till7.net/2007/05/03/using-ajax-to-spiff-up-a-gallery/</link>
		<comments>http://www.3till7.net/2007/05/03/using-ajax-to-spiff-up-a-gallery/#comments</comments>
		<pubDate>Fri, 04 May 2007 00:52:03 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web development]]></category>

		<category><![CDATA[ajax]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.3till7.net/2007/05/03/using-ajax-to-spiff-up-a-gallery/</guid>
		<description><![CDATA[Lightbox is nice and all, but sometimes I find it clunky, particularly in Firefox when I have a lot of other tabs open.  However, I don't want to go the route of having an image load by itself in the browser, because it would be so much nicer to have it integrated into my [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.huddletogether.com/projects/lightbox/">Lightbox</a> is nice and all, but sometimes I find it clunky, particularly in Firefox when I have a lot of other tabs open.  However, I don't want to go the route of having an image load by itself in the browser, because it would be so much nicer to have it integrated into my layout.  I could have a whole bunch of separate pages, one for each image, and do a full page load when a thumbnail is clicked, but that's kind of a hassle.  Well, with a simple PHP script and some AJAX calls, I can have my image load within the gallery page without a page reload.</p>
<h3 class="space_above">Things You'll Need</h3>
<ul class="padded">
<li><a href="http://www.prototypejs.org/">Prototype</a> for its <a href="http://prototypejs.org/api/ajax/updater">Ajax.Updater</a>, which will be used to update an element in the current page without reloading the page.</li>
<li>You'll need PHP in order to write the script to parse URL parameters and display an image.</li>
</ul>
<h3 class="space_above">Image Displayer</h3>
<p>If you try to load an image in the middle of an HTML page, your browser is going to lug along before displaying a whole bunch of crap that is the actual image source.  That is, you won't see your pretty image.  Instead, you need to load another HTML page that has the image in it.  Instead of having a separate page for every image in your gallery, we're just going to have one boilerplate PHP script that will take parameters in the URL and display an image:</p>
<div class="synthi_code" style="display:block;" id="styled_synthi_48b733813dc48">
<h2 class="synthi_header"> PHP</h2>
<div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'src'</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp;<span style="color: #0000ff;">$src</span> = <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'src'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'alt'</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp;<span style="color: #0000ff;">$alt</span> = <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/stripslashes"><span style="color: #000066;">stripslashes</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'alt'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;<br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'width'</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp;<span style="color: #0000ff;">$width</span> = <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'width'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'height'</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp;<span style="color: #0000ff;">$height</span> = <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'height'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'desc'</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><br />
&nbsp;<span style="color: #0000ff;">$desc</span> = <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/stripslashes"><span style="color: #000066;">stripslashes</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'desc'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #b1b100;">else</span><br />
&nbsp;<span style="color: #0000ff;">$desc</span> = <span style="color: #0000ff;">$alt</span>;</p>
<p><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'max_width'</span>, <span style="color: #0000ff;">$_GET</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp;<span style="color: #0000ff;">$max_width</span> = <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'max_width'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;<br />
&nbsp;<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$width</span> &gt; <span style="color: #0000ff;">$max_width</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">$width</span> = <span style="color: #0000ff;">$max_width</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">$height</span> = <span style="color: #ff0000;">''</span>;<br />
&nbsp;<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;a href=<span style="color: #ff0000;">&quot;&lt;?php echo $src; ?&gt;&quot;</span>&gt;&lt;img src=<span style="color: #ff0000;">&quot;&lt;?php echo $src; ?&gt;&quot;</span> alt=<span style="color: #ff0000;">&quot;&lt;?php echo $alt; ?&gt;&quot;</span> width=<span style="color: #ff0000;">&quot;&lt;?php echo $width; ?&gt;&quot;</span> height=<span style="color: #ff0000;">&quot;&lt;?php echo $height; ?&gt;&quot;</span> /&gt;&lt;/a&gt;&lt;br /&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$desc</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span> |<br />
&lt;a href=<span style="color: #ff0000;">&quot;&lt;?php echo $src; ?&gt;&quot;</span>&gt;View just the image &amp;raquo;&lt;/a&gt;</div>
</div>
<p>What this script is doing is checking to see if any of our desired parameters (e.g. width, alt) were passed in the URL, which is stored in PHP's <code>$_GET</code> <a href="http://www.php.net/manual/en/language.variables.predefined.php">superglobal</a>.  If they were, then the function calls to <code>array_key_exists()</code> will return <code>TRUE</code>, and a variable will be set for each of our attributes.  The calls to <code>htmlspecialchars()</code> is a safety check to turn any code, such as malicious Javascript, into harmless ASCII entities (e.g. it replaces &lt; and &gt; with &amp;lt; and &amp;gt;).  The calls to <code>stripslashes()</code> is to ensure any added \'s before quotes are removed before the text gets printed out.</p>
<p>After the data is gathered in this script, it is then printed out in appropriate HTML tags.  The image is stuffed into an <code>img</code> tag, the description is printed out, etc.  Now we can link to this script, which I named image_displayer.php, and pass in the appropriate values in the URL with this format: <code>image_displayer.php?<em>key_1=value_1&#038;key_2=value_2</em></code> and so on with as many values as we want to define.  In PHP, if a variable is not initialized to some value, if it is later printed, it just doesn't print anything at all, rather than giving an error.</p>
<h3 class="space_above">Javascript</h3>
<p>Make sure you've included the Prototype Javascript library.  Then, you'll need to create some HTML element, probably a <code>div</code>, and give it a unique ID.  E.g. <code>&lt;div id="gallery_photo"&gt;&lt;/div&gt;</code>--note that we're leaving it empty.  Now, outside of that div, you can make the links to your images:</p>
<blockquote><p><code>&lt;a href="/wp-content/da/blobs1.jpg" title="Prisms" onclick="new Ajax.Updater('photo_display', '/wp-content/image_displayer.php?max_width=700&amp;width=700&amp;height=662&amp;alt=Prisms&amp;src=/wp-content/da/blobs1.jpg', {asynchronous:true, method:'get'}); return false;"&gt;</code><br />
<code>&lt;img src="/wp-content/da/thumb/blobs1.jpg" alt="Prisms" style="width: 40px; height: 40px; border: none;"/&gt;</code><br />
<code>&lt;/a&gt;</code></p></blockquote>
<p>To break it down, we include the image URL in the regular <code>href</code> section so that if Javascript is not enabled, the user will be taken to the image directly.  The <code>onclick</code> section is where the magic happens.  We're making a call to Ajax.Updater and telling it 1) the ID of the HTML element that we want to stick the result of our request, 2) the URL to our image_displayer.php to have our image displayed, and 3) that we want the request to happen asynchronously and we want to use the GET method as opposed to POST, which is typically used for form submissions.</p>
<p>And there you are.  :)  You can see this in action in my <a href="/graphic-design/?PHPSESSID=3a61a54546ee031cac1cb118b3d174e7">Graphic design</a> section.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3till7.net/2007/05/03/using-ajax-to-spiff-up-a-gallery/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
