Drupal7 AJAX Forms


Although IMHO Drupal's spin on AJAX is still fairly horrible compared with the amazing Partial Page Rendering/Update technology of JavaServer Faces (JSF) and 3rd party JSF toolkits like Primefaces, the Drupal7 Forms API makes it easier than previously to handle AJAX updates. From Form API Changes for Drupal 7, Part 2: AJAX/AHAH Changes:

One of the many pleasant improvements to the Drupal Form API is that AJAX Forms are now really easy. Really easy. We used to call them AHAH forms, and the concept was the same, but they were so complicated that few people attempted them. In D7 we even changed the name to distance ourselves from "AHAH" :-)

'Really easy' ?

Well, no, just a lot easier in Drupal7 than it was in Drupal6. As a huge fan of (and very experienced with) JavaServer Faces (JSF2), I think it's fair to say that JSF2 users have enjoyed using elegant, sophisticated, powerful AJAX for many years as easily as spreading warm butter on toast. As usual, certain "I live in a tiny Drupal fishbowl and have just discovered programming and made a web site and am so excited" Drupal Religion fanatics are easily impressed, because they have clearly not seen and experienced other Information Technologies (IT). I have experienced both; I know how the Drupal7 AJAX facilities work, and JavaServer Faces (JSF2) wins hands down, no question, no argument.

Dr Darren, Webel IT Australia

But back to Drupal (this is a Drupal7 site after all). From Ajax Forms in Drupal 7:

'Ajax-enabled forms in Drupal 7 offer dynamic form behavior with no page reloads and are easy to create and manipulate. They are a simple extension of the Drupal Form API.

What is dynamic behavior? Traditional web behavior has the user fill in a form, click a button, and the entire page is rebuilt and sent back to the browser. Ajax-enabled forms update or replace part of the page or part of the form without doing a full page reload - only the part that needs to be changed is changed. It's more responsive to the user and typically faster than the traditional page reload approach.'

It is worth noting that:

'Before Drupal 7, Ajax forms were referred to as AHAH forms because Ajax (Asynchronous JavaScript and XML) implied that XML was involved but the Drupal technique doesn't use any XML (and the JavaScript part is behind the scenes). In Drupal 7 the terminology was changed to use the common and recognizable "Ajax" even though it is not literally exact'

Core Drupalers tend to use the word 'Ajax' rather than the almost-acronym 'AJAX'.

Did you know ? HOWTO keep form state between non-JavaScript fallback form building

When dealing with AJAX Forms handling fallback to non-JavaScript mode can be subtle. From Drupal 7 multistep forms using variable functions:

In Drupal 6 to carry data back to your form builder you set the storage key of $form_state in your submit handler. In Drupal 7, upon return to your builder after submission, you carry data over by keeping the Form API from pulling the form array out of cache*. You do so by setting $form_state['rebuild'] to TRUE in your validate or submit handlers