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.








Article comments
1 - Aaman
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
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."