ajax_get_form

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 ajax_get_form()

Get a form submitted via #ajax during an Ajax callback.

This will load a form from the form cache used during Ajax operations. It pulls the form info from $_POST.

Return value

An array containing the $form and $form_state. Use the list() function to break these apart:

<?php
    list($form, $form_state, $form_id, $form_build_id) = ajax_get_form();
  ?>

Related topics

▾ 2 functions call ajax_get_form()

ajax_form_callback in includes/ajax.inc
Menu callback; handles Ajax requests for the #ajax Form API property.
file_ajax_upload in modules/file/file.module
Menu callback; Shared Ajax callback for file uploads and deletions.

Code

includes/ajax.inc, line 323

<?php
function ajax_get_form() {
  $form_state = form_state_defaults();

  $form_build_id = $_POST['form_build_id'];

  // Get the form from the cache.
  $form = form_get_cache($form_build_id, $form_state);
  if (!$form) {
    // If $form cannot be loaded from the cache, the form_build_id in $_POST
    // must be invalid, which means that someone performed a POST request onto
    // system/ajax without actually viewing the concerned form in the browser.
    // This is likely a hacking attempt as it never happens under normal
    // circumstances, so we just do nothing.
    watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING);
    drupal_exit();
  }

  // Since some of the submit handlers are run, redirects need to be disabled.
  $form_state['no_redirect'] = TRUE;

  // When a form is rebuilt after Ajax processing, its #build_id and #action
  // should not change.
  // @see drupal_rebuild_form()
  $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
  $form_state['rebuild_info']['copy']['#action'] = TRUE;

  // The form needs to be processed; prepare for that by setting a few internal
  // variables.
  $form_state['input'] = $_POST;
  $form_id = $form['#form_id'];

  return array($form, $form_state, $form_id, $form_build_id);
}
?>