Easy Ajax: A Case Study - Page 6

UPDATE mt_workflow SET workflow_closed_on=NOW(),workflow_created_by=$editor_id WHERE workflow_entry_id=$entry_id AND workflow_type=1

If a record does exist for someone else, I might still be okay. If it's an old record — defined in this case as 130 seconds old, but rounded to the minute — then I take the "Old, so me!" clause. The old user has apparently navigated away from the window or closed their browser, so I clear out the old record and create one for the current user instead:

DELETE FROM mt_workflow WHERE workflow_entry_id=$entry_id AND workflow_type=1

INSERT INTO mt_workflow VALUES (0,$entry_id,$editor_id,1,NULL,$editor_id,NOW(),NULL,NOW())

Everything Else

There is nothing else. Ajax programming is as simple as setting up a Javascript timer, creating an XMLHttpRequest, object, and handling the result when that object returns. As long as you think about timing issues and keep an eye on memory usage, you're now well on your way.

Bad Code

The sharp-eyed observer will note that if it take longer than one second for keepalive.php to return, things could get pretty busy. Fortunately in this case, it's a very fast server. I've considered setting a flag when I open the object and clearing it when the object returns, and ignoring the flag if several seconds have passed, but so far it hasn't caused any problems, and I'd rather avoid the added complexity.

Page 1Page 2Page 3Page 4Page 5 — Page 6

Article tags

Spread the word
Bookmark and Share
Profile image for phillip-winn

Article Author: Phillip Winn

Phillip Winn is the Chief Geek for Blogcritics, and a blogger since 1995. He may currently be found and followed as @pwinn on Twitter.

Visit Phillip Winn's author pagePhillip Winn's Blog

Read comments on this article, and add some feedback of your own

Article comments

  • 1 - Aaman

    Dec 31, 2005 at 12:28 am

    I wish that Ajax book was cheaper:)

    Incidentally, is this true AJAX, does the reliance on PHP and SQL not mean a new paradigm, not reliant on XML?

  • 2 - Phillip Winn

    Dec 31, 2005 at 11:49 am

    It's still XMLHttpRequest, so all three elements of AJAX are present. The PHP and Mysql are, I think, incidental. It could as easily be anything else, or an external webservice, on the "back end."

Add your comment, speak your mind

Personal attacks are NOT allowed.
Please read our comment policy.
Please preview your comment.

blogcritics lists for Nov 21, 2009

fresh articles Most recent articles site-wide

fresh comments Most recent comments site-wide

most comments Most comments in 24hrs

top writers Most prolific Blogcritics for October

top commenters Most prolific Commenters in 24 hrs