AJAX First Steps

Written on December 24, 2006 – 9:32 pm | by John |

So, yeah, its Christmas eve...and guess what I had some extra time this afternoon! So, I decided to take some of the initial steps to learn AJAX. I decided to start off with prototype. For a pretty simple example I found a quick zip code lookup tutorial by Pete Freitag.

Here are the peices/parts:

The HTML

HTML:
  1. <link rel="stylesheet" type="text/css" /> <script type="text/javascript"></script> <script language="javascript" type="text/javascript"></script> <form>     zip:
  2. <input style="background-color: #ffffa0" name="zip" id="zip" onkeyup="checkZip();" type="text" /> </form>

The JavaScript (ZipCode.js)

JavaScript:
  1. function checkZip() {
  2. if($F('zip').length == 5) {
  3. var url = 'checkZip.cfm';
  4. var params = 'zip=' + $F('zip');
  5. var fullUrl = url + "&amp;" + params;
  6. var ajax = new Ajax.Updater(
  7. {success: 'zipResult'},
  8. url,
  9. {method: 'get', parameters: params, onFailure: reportError});
  10. }
  11. }
  12. function reportError(request) {
  13. $F('zipResult') = "Error";
  14. }

The ColdFusion (checkZip.cfm)

HTML:
  1. <p class="badzip">Please enter a valid zip code.
  2.  
  3. SELECT name, state FROM zips
  4. WHERE zip =
  5.  
  6. </p><p class="goodzip">#ziplookup.name#, #ziplookup.state#
  7.  
  8. </p><p class="badzip">Zip code not found.

There is also a zip code db (MySQL) for the data end of things.

Considering the lack of code I'm thinkin' that is pretty slick to get external data into an HTML page with no page refresh. I'm pleased so far.

Helpful resources found so far:
Quick Guide: http://particletree.com/features/quick-guide-to-prototype/
Scriptaculous Wikie for Prototype: http://wiki.script.aculo.us/scriptaculous/show/Prototype
Unofficial Prototype documentation: http://www.sergiopereira.com/articles/prototype.js.html#UsingAjaxUpdater

Merry Christmas!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • description
  • ThisNext
  • MisterWong
  • Wists
  • Facebook
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Related Posts

Put your related posts code here
Find entries :

Want to subscribe?

  Subscribe in a reader or,
Subscribe via email:

About me

I'm a senior developer at RealEyes Media, Adobe Certified Instructor and Adobe Certified Professional. Here you'll get my ideas and experience Flex, Flash, ColdFusion and related technologies as well as some generally off the wall stuff.