This page shows the workflow of a form across multiple page requests, including AJAX requests and form rebuilds. This applies to both Drupal 6.x and 7.x.

Form without AJAX

Let's start with a form without any #ajax or #ahah elements:

workflow without ajax 1

Once the user has gone through these two page loads, the form is cached. Thus any further form rebuilds now work like this:

workflow without ajax 2

Form with AJAX

When a form includes some #ajax or #ahah elements, form caching is automatically turned on. This means the form is already cached when the user loads the form the first time. This changes the workflow a bit, as the second page load may already be served from cache. When the AJAX request is served, the form is also retrieved from cache, submitted and rebuilt. Thus the form is constructed again and then passed to any defined AJAX callback, which then returns the changes to the Javascript.

The updated form is cached internally and the form representation gets updated with the help of Javascript. Thus when the user submits the data, the updated form gets retrieved from cache so the API can process and submit the updated form.

For Drupal 6 this might look a bit different, as the AHAH callback has to implement all the form magic on its own; the details of this vary depending on the callback. But usually, the form is retrieved from cache, processed, and then a new form is prepared, cached and rendered -- which is pretty much the same as in Drupal 7.

workflow with ajax 1

Any further page loads are now served from cache, regardless of whether the AJAX callback is triggered in between or not:

workflow with ajax 2