I've got a simple form which I've added a little AJAX to. The form simply let's people add users to a list. That is, I've got a textfield where people can enter an e-mail address, and that e-mail address gets added to the list.

Now, the "Add" button is AJAX enabled, meaning that the add request gets posted as an AJAX request. However, I would then like the AJAX callback function to render the form again so that the newly added user appears in the list. However, I can't really see how I should do this.

I've tried to build the form ("from scratch") using drupal_get_form() in the AJAX callback function and then return that form, but that does not work. It just gives me the error :

Notice: Undefined index: form_build_id in /home/playground/www/public_html/includes/ajax.inc on line 320

How should I go about when I would like to rebuild the form from the very scratch as a part of the AJAX request, and then return that form?

2 Answers 2


In your AJAX callback, use $form_state['rebuild'] = TRUE; (drupal_rebuild_form).

$form_state['rebuild'] is very useful in multistep forms, but it can also be handy in Ajax callbacks.

I recommend checking out the Examples project as well. Both the Form examples and the AJAX examples.

If you will add the relevant snippets of your code I will try to add more details.

  • 3
    Thank's for that! I have built multistep forms before, and I've often used the $form_state['rebuild'], but I what I did not remember was that i needed to set it in the submit callback and not in the ajax callback :-) Thanks for pointing me onto the right track!
    – sbrattla
    Commented Aug 21, 2012 at 7:03
  • 1
    I realize this answer is quite old, but I wonder: How is setting $form_state['rebuild'] in an AJAX callback supposed to have any effect? If you look at ajax_form_callback you can clearly see that the form has already been processed once the callback gets invoked. Commented Jan 22, 2016 at 21:54
  • Downvote because @ciss is right - the ajax callback is called to late to have any effect.
    – Felix Eve
    Commented Aug 31, 2017 at 3:49

Looks like you're trying to do it old way.

In D7 your form building code runs on initial form display and on subsequent ajax calls. You can make use of $form_state parameter there to render the form according to freshly entered data.

You attach a '#callback' attribute to ajaxified elements. Most examples show returning one particular element in a callback. But it's just a convenient special case. There is a powerful Ajax Commands API, it can be used to request execution of a wide range of operations on a client from a server.

No need to manually rebuilding forms or something like that. Just look at the examples on http://api.drupal.org/api/drupal/includes!ajax.inc/group/ajax_commands/7.

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.