Im creating a dynamic form, using FORM API. Several lines of results, fetched from DB, and several functionnalities like deleting a row, adding a row, modifying quantites and calculating total prices.

I did everything using ajax callback, but Im not sure this is the best way of doing it, especially with the 'change quantities' and 'calculating prices' functionnalities.

What I understand about AJAX Callbacks is that a full refresh of the form is done during callback (using cache if possible), and that alony a certain part of the form is returned (using return $form[XXXX]).

So when I change a quantity input field, the form is rebuild, and Im updating the price col and total.

The question im asking to myself is : should I use simple javascript to manage my prices when quantities change ? For me its seems more logical, avoiding by the way useless PHP requests.

As Im not sure about it, likely to forget something important, I prefer asking it to you guys. Thx !

1 Answer 1


I build such a dynamic form recently and asked me the same question. I did go with the Drupal-way and only used the Form API with zero lines of custom JavaScript. In general you have to weigh the pros and cons of server-side vs. client-side processing. For me these are:

Form API approach

  • (+) No custom JavaScript/jQuery code neccessary.
  • (+) You have the full power of the Form API including validation and storing of data inside $form_state (This was one of the main reasons for me since I was using this inside a multi-step form).
  • (+) Logic stays on the server. You probably need some server-side functions or do not want to expose your logic to everyone.
  • (-) It will be slower then a JS approach (depends on your server's performance).

JavaScript approach

  • (+) Fast. Since you do all processing on the client, you can update values instantly.
  • (+) May be easier to implement. It can be more straight-forward since you don't need any deep Form API knowledge.
  • (-) You have to fiddle around with jQuery selectors to somehow pass data to Drupal (maybe using hidden fields). This is not always nice and things can break if the HTML structure changes.

Normally I would prefer a JavaScript approach since the user experience can be much better. But in my recent case I really liked it to not have to write any custom JS code and let Drupal handle all the AJAX stuff. I was really impressed by the capabilities of the Form API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.