Cannot get AJAX callback values to save I am using Drupal 7, and I have a content type called Case Record that has the following fields:


Title  (title, Node module element)
Case Number (field_cr_case_number, Text)
Case Title (field_cr_case_title, Text)


When the user creates a new node of this type and enters in a Case Number, an AJAX call is supposed to fetch the Case Title and populate it on the Case Title textbox. Here is my module code:

&lt;?php
/**
 * @file
 * Module for Case Records.
 *
 * @category Examples
 *
 * @package Example Modules
 */

/**
 * Implements hook_form_alter().
 */
function case_records_database_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch ($form_id) {
    case 'case_record_node_form':

      $form['field_cr_case_number'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; 'Case Number',
        '#prefix' =&gt; '&lt;div id="div-case-number-element-wrapper"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',
        '#ajax' =&gt; array(
          'callback' =&gt; 'case_records_database_ajax_callback',
          'event' =&gt; 'change',
          'method' =&gt; 'replace',
          'wrapper' =&gt; 'div-case-name-element-wrapper',
        ),
      );

      $cs_short_title = "";
      if (isset($form_state['values']['field_cr_case_number'])) {

        // Code omitted. Use db_select to query the database and
        // get the real case title, but for now we'll just call it
        // "Some Case Title".
        $cs_short_title = "Some Case Title";
      }

      $form['field_cr_case_title'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('Case Title'),
        '#value' =&gt; $cs_short_title,
        '#prefix' =&gt; '&lt;div id="div-case-name-element-wrapper"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',
      );

      break;
  }
}

/**
 * AJAX callback.
 */
function case_records_database_ajax_callback($form, &amp;$form_state) {
  return $form['field_cr_case_title'];
}


The AJAX call does indeed populate the Case Title text field with the dummy "Some Case Title" text. However, when I save the new node neither the Case Number field nor the Case Title field are saved with any values. They are both blank. What is wrong with my code?
That code is overriding the fields structure. That fields have a specific structure and that code is changing it completely, so in submit the values are not recognized. 

I recommend you use devel module and use dpm function to inspect the structure of that fields:

  dpm($form['field_cr_case_number']);
  dpm($form['field_cr_case_title']);


To make your form changes you must only add the #ajax property to your field:

$form['field_cr_case_number'][LANGUAGE_NONE][0]['value']['#ajax'] = array(
  'callback' =&gt; 'case_records_database_ajax_callback',
  'event' =&gt; 'change',
  'method' =&gt; 'replace',
  'wrapper' =&gt; 'div-case-name-element-wrapper',
);


And to add the wrapper and #value to your second field do something like this:

$form['field_cr_case_title'][LANGUAGE_NONE][0]['value']['#prefix'] = '&lt;div id="div-case-name-element-wrapper"&gt;';
$form['field_cr_case_title'][LANGUAGE_NONE][0]['value']['#suffix'] = '&lt;/div&gt;';
$form['field_cr_case_title'][LANGUAGE_NONE][0]['value']['#value'] = $cs_short_title;


In this question is also explained how to make the ajax callback.