My first AJAX project
January 2nd, 2008AJAX is a buzzword. It’s a method for sending data to the server like you would in a form or a URL, but you do it without refreshing the page. It used to be that you would have to send data to the server by clicking a link or submitting a form; then the server would respond with a new page that would load in your browser window. With AJAX, you can send data, get it back and change just parts of the page, instead of reloading the whole thing.
This improves a site’s overall server load by reducing the need to download whole pages at a time. But, more importantly, you can design pages that are WAY more functional.
Some of you might be Facebook users. If you try to search for a friend in the search field on Facebook, every time you press a key on your keyboard, that letter is sent to Facebook’s server. The server turns it around by looking up your friends names, and it tries to figure out who you’re searching for. For each match, it sends the name and a clickable box back to your browser window. Try it out. Start typing a friend’s name and watch how it starts to appear near your cursor. Pretty cool, huh?
Remember the AJAX part of that is just the connection between the browser window and the server. Everything else happens with JavaScript (on the browser end) and PHP/MySQL (on the server end).
So what about my first AJAX site?
Go to www.venuspoetry.com and click anywhere in the painting. You’ll find yourself with either a poem or a page where a poem is waiting to be started. Nothing special about that, you decide. But then you see what happens when you click on a poem.
First, a JavaScript function toggles the formatted text into a TEXTAREA so you can type. A button appears with the value “Close” so you can declare when you’re finished.
Then, a JavaScript function toggles the TEXTAREA and the user input within back into formatted text on the page. Also, a CAPTCHA API (called reCAPTCHA, see this post) magically appears over a link that says “Save this poem.” When you click save, all of the data you changed, plus the reCAPTCHA authentication gets sent to the server.
Then, the server checks the CAPTCHA validation first (and rejects your post if it’s wrong) and then adds your post to the database.
A line saying your work was saved or not saved gets sent back to the browser.
Then, the line is posted to the page. If the save worked, it locks out further editing and removes the CAPTCHA. If it didn’t work, you are prompted to reenter the CAPTCHA and click save again.
Most of this happens with JavaScript and the DOM, which is really fun. Pages can be so much more interactive and functional. AJAX applications act much more like regular desktop applications.
Unfortunately, you can only submit one edit … and that’s something I had to reduce back to because of the way reCAPTCHA works. I suppose there’s a way to reload it automatically, but I haven’t started messing with it yet. I was happy to get it all working last night.





