login
jQuery AJAX chaining posted: Wed 2013-05-22 21:15:07 tags: tech
So jQuery's AJAX is pretty cool, but I've hit another wall.

As discussed under the entry on collision check patterns, I'm working with an app that has to first check the data in the browser form fields against the database to make sure we're not in danger of accidentally overwriting existing data with a mistaken identifier. I use jQuery's $.post() to POST a key identifying form datum to a piece of PHP middleware, let's call it Collision.php. If there is a collision, then Collision.php responds with complementary identifying data from the pre-existing record. If there's no collision, the middleware returns 0.

So far so good. Either way, the JavaScript is correctly using the data returned by the AJAX call. Now in the event of no collision, the code proceeds to another step: POSTing complete form data to a different PHP middleware script, let's call it SaveRecord.php. The PHP does everything it's supposed to: INSERTs the record into the database, retrieving the newly-assigned record ID number, and returning it to the calling JavaScript. Here's where the problem arises: the JavaScript just isn't "hearing" what SaveRecord.php "says".