Information available to an AJAX callback function on a form

  • With this code we can see that we have access to the entire form (output, which comes to us as a JavaScript error alort, below).

    <?php
    function formmsgs_image_style_name($form, $form_state) {
      die(
    var_export($form,TRUE));
    }
    ?>

    Crucially, it also gives us the value that has been entered into the (unsubmitted) form. Here, that is 'head shot'.

    An AJAX HTTP error occurred.
    HTTP Result Code: 200
    Debugging information follows.
    Path: /system/ajax
    StatusText: OK
    ResponseText: array (
    'name' => 
    array (
    '#type' => 'textfield',
    '#size' => '64',
    '#title' => 'Style name',
    '#default_value' => '',
    '#description' => 'The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).',
    '#element_validate' => 
    array (
    0 => 'image_style_name_validate',
    ),
    '#required' => true,
    '#ajax' => 
    array (
    'callback' => 'formmsgs_image_style_name',
    'event' => 'keyup',
    'wrapper' => 'formmsgs-image-style-name',
    ),
    '#suffix' => 'Default message.',
    '#input' => true,
    '#maxlength' => 128,
    '#autocomplete_path' => false,
    '#process' => 
    array (
    0 => 'ajax_process_form',
    ),
    '#theme' => 'textfield',
    '#theme_wrappers' => 
    array (
    0 => 'form_element',
    ),
    '#defaults_loaded' => true,
    '#tree' => false,
    '#parents' => 
    array (
    0 => 'name',
    ),
    '#array_parents' => 
    array (
    0 => 'name',
    ),
    '#weight' => 0,
    '#processed' => true,
    '#attributes' => 
    array (
    ),
    '#title_display' => 'before',
    '#id' => 'edit-name--2',
    '#name' => 'name',
    '#value' => 'head shot',
    '#needs_validation' => true,
    '#ajax_processed' => true,
    '#attached' => 
    array (
    'library' => 
    array (
    0 => 
    array (
    0 => 'system',
    1 => 'jquery.form',
    ),
    1 => 
    array (
    0 => 'system',
    1 => 'drupal.ajax',
    ),
    ),
    'js' => 
    array (
    0 => 
    array (
    'type' => 'setting',
    'data' => 
    array (
    'ajax' => 
    array (
    'edit-name--2' => 
    array (
    'callback' => 'formmsgs_image_style_name',
    'event' => 'keyup',
    'wrapper' => 'formmsgs-image-style-name',
    'url' => '/system/ajax',
    'submit' => 
    array (
    '_triggering_element_name' => 'name',
    ),
    ),
    ),
    ),
    ),
    ),
    ),
    '#sorted' => true,
    ),
    'submit' => 
    array (
    '#type' => 'submit',
    '#value' => 'Create new style',
    '#input' => true,
    '#name' => 'op',
    '#button_type' => 'submit',
    '#executes_submit_callback' => true,
    '#limit_validation_errors' => false,
    '#process' => 
    array (
    0 => 'ajax_process_form',
    ),
    '#theme_wrappers' => 
    array (
    0 => 'button',
    ),
    '#defaults_loaded' => true,
    '#tree' => false,
    '#parents' => 
    array (
    0 => 'submit',
    ),
    '#array_parents' => 
    array (
    0 => 'submit',
    ),
    '#weight' => 0.001,
    '#processed' => true,
    '#required' => false,
    '#attributes' => 
    array (
    ),
    '#title_display' => 'before',
    '#id' => 'edit-submit--2',
    '#ajax_processed' => false,
    '#sorted' => true,
    ),
    '#form_id' => 'image_style_add_form',
    '#build_id' => 'form-AOQK0kh-ZfbkPMEFdRVcLmPB3kgqjtf19rypnqYajkQ',
    '#action' => '/admin/config/media/image-styles/add',
    '#type' => 'form',
    'form_build_id' => 
    array (
    '#type' => 'hidden',
    '#value' => 'form-AOQK0kh-ZfbkPMEFdRVcLmPB3kgqjtf19rypnqYajkQ',
    '#id' => 'form-AOQK0kh-ZfbkPMEFdRVcLmPB3kgqjtf19rypnqYajkQ',
    '#name' => 'form_build_id',
    '#input' => true,
    '#process' => 
    array (
    0 => 'ajax_process_form',
    ),
    '#theme' => 'hidden',
    '#defaults_loaded' => true,
    '#tree' => false,
    '#parents' => 
    array (
    0 => 'form_build_id',
    ),
    '#array_parents' => 
    array (
    0 => 'form_build_id',
    ),
    '#weight' => 0.002,
    '#processed' => true,
    '#required' => false,
    '#attributes' => 
    array (
    ),
    '#title_display' => 'before',
    '#ajax_processed' => false,
    '#sorted' => true,
    ),
    '#token' => 'image_style_add_form',
    'form_token' => 
    array (
    '#id' => 'edit-image-style-add-form-form-token',
    '#type' => 'token',
    '#default_value' => 'pWL1C-vAdfinCfYuAF4zM_cH3cX_rPM0GuJzQzXZML4',
    '#input' => true,
    '#theme' => 'hidden',
    '#defaults_loaded' => true,
    '#tree' => false,
    '#parents' => 
    array (
    0 => 'form_token',
    ),
    '#array_parents' => 
    array (
    0 => 'form_token',
    ),
    '#weight' => 0.003,
    '#processed' => false,
    '#required' => false,
    '#attributes' => 
    array (
    ),
    '#title_display' => 'before',
    '#name' => 'form_token',
    '#value' => 'pWL1C-vAdfinCfYuAF4zM_cH3cX_rPM0GuJzQzXZML4',
    '#needs_validation' => true,
    '#sorted' => true,
    ),
    'form_id' => 
    array (
    '#type' => 'hidden',
    '#value' => 'image_style_add_form',
    '#id' => 'edit-image-style-add-form',
    '#input' => true,
    '#process' => 
    array (
    0 => 'ajax_process_form',
    ),
    '#theme' => 'hidden',
    '#defaults_loaded' => true,
    '#tree' => false,
    '#parents' => 
    array (
    0 => 'form_id',
    ),
    '#array_parents' => 
    array (
    0 => 'form_id',
    ),
    '#weight' => 0.004,
    '#processed' => true,
    '#required' => false,
    '#attributes' => 
    array (
    ),
    '#title_display' => 'before',
    '#name' => 'form_id',
    '#ajax_processed' => false,
    '#sorted' => true,
    ),
    '#id' => 'image-style-add-form--2',
    '#method' => 'post',
    '#theme_wrappers' => 
    array (
    0 => 'form',
    ),
    '#tree' => false,
    '#parents' => 
    array (
    ),
    '#submit' => 
    array (
    0 => 'image_style_add_form_submit',
    ),
    '#theme' => 
    array (
    0 => 'image_style_add_form',
    ),
    '#processed' => false,
    '#defaults_loaded' => true,
    '#required' => false,
    '#attributes' => 
    array (
    ),
    '#title_display' => 'before',
    '#array_parents' => 
    array (
    ),
    '#sorted' => true,
    )