Drupal nested ajax call not working correctly I got this nested ajax call issue,


/**
 * Problems
 * 1). When I click "Add one more" to add more file, I need to click the button
 *     Twice to get one 'Name'/file element available. I click once, no file 
 *     field shows. Click again, one file field will show.
 * 2). When one file field shows, as long as I click 'upload' button of the 'managed_file'
 *     field, this file field will disappear immediately. I upload another file, this
 *     file will be fine. Then another uploaded file will disappear.
 * 3). When I click "submit" to submit the form, all previously disappeared file fields
 *     will show. Because, as you see, the submit function only rebuild the form.
 * 4). If container of multiple file fields are not triggered by ajax, i.e,
 *     $form['test']['names_fieldset'] is not triggered by "select" field, then
 *     tihs "Add one more" function works perfectly.
 *
 * So, why this happens with the ajax triggered container:)?
 */
function agl_jira_new_test_form($form, &amp;$form_state) {
  $form['select'] = array(
    '#title' =&gt; 'select',
    '#type' =&gt; 'select',
    '#options' =&gt; drupal_map_assoc(array('man', 'woman')),
    '#required' =&gt; TRUE,
    '#default_value' =&gt; isset($form_state['values']['select']) ? $form_state['values']['select'] : NULL,
    '#ajax' =&gt; array(
      'callback' =&gt; 'agl_jira_select_callback',
      'wrapper' =&gt; 'names-fieldset-wrapper',
    ),
  );
  $form['test']['names_fieldset'] = array(
    '#markup' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;&lt;/div&gt;',
  );
  if (isset($form_state['values']['select']) &amp;&amp; $form_state['values']['select'] == 'man') {
    $form['test']['names_fieldset'] = array(
      '#type' =&gt; 'fieldset',
      '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
      '#tree' =&gt; TRUE,
    );
    $form['test']['names_fieldset']['text'] = array(
      '#title' =&gt; t('name'),
      '#type' =&gt; 'textfield',
    );
    $form['test']['names_fieldset']['attachment'] = array(
      '#title' =&gt; t('File'),
      '#type' =&gt; 'fieldset',
      '#prefix' =&gt; '&lt;div id="agl-jira-file-attach-wrapper"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
    );
    if (!isset($form_state['num_names'])) {
      $form_state['num_names'] = 1;
    }
    for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
      $form['test']['names_fieldset']['attachment']['name'][$i] = array(
        '#type' =&gt; 'managed_file',
        '#title' =&gt; t('Name'),
      );
    }
    $form['test']['names_fieldset']['add_name'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Add one more'),
      '#limit_validation_errors' =&gt; array(),
      '#submit' =&gt; array('agl_jira_example_add_more_add_one'),
      '#ajax' =&gt; array(
        'callback' =&gt; 'agl_jira_example_add_more_callback',
        'wrapper' =&gt; 'agl-jira-file-attach-wrapper',
      ),
    );
  }
  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'submit',
  );
  return $form;
}

function agl_jira_select_callback($form, $form_state) {
  return $form['test']['names_fieldset'];
}

function agl_jira_new_test_form_submit($form, &amp;$form_state) {
  //dsm('form has been submitted!');
  // do something.
  $form_state['rebuild'] = TRUE;
}

function agl_jira_example_add_more_add_one($form, &amp;$form_state) {
 // dsm('submitted');
  $form_state['num_names']++;
  $form_state['rebuild'] = TRUE;
}

function agl_jira_example_add_more_callback($form, $form_state) {
  return $form['test']['names_fieldset']['attachment'];
}


Any help will be appreciated.
Correct one, just copy and paste it:

function agl_jira_new_test_form($form, &amp;$form_state) {
  $form['#tree'] = TRUE;
  $form['select'] = array(
    '#title' =&gt; 'select',
    '#type' =&gt; 'select',
    '#options' =&gt; drupal_map_assoc(array('man', 'woman')),
    '#required' =&gt; TRUE,
    '#default_value' =&gt; isset($form_state['values']['select']) ? $form_state['values']['select'] : 'man',
    '#ajax' =&gt; array(
      'callback' =&gt; 'agl_jira_select_callback',
      'wrapper' =&gt; 'names-fieldset-wrapper',
    ),
  );
  $form['test']['names_fieldset'] = array(
    '#type' =&gt; 'fieldset',
    '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#tree' =&gt; TRUE,
  );
  if (isset($form_state['values']['select']) &amp;&amp; $form_state['values']['select'] == 'man') {
    $form['test']['names_fieldset']['text'] = array(
      '#title' =&gt; t('name'),
      '#type' =&gt; 'textfield',
    );
    $form['test']['names_fieldset']['attachment'] = array(
      '#title' =&gt; t('File'),
      '#type' =&gt; 'fieldset',
      '#prefix' =&gt; '&lt;div id="agl-jira-file-attach-wrapper"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
      '#tree' =&gt; TRUE,
    );
    if (!isset($form_state['num_names'])) {
      $form_state['num_names'] = 1;
    }
    for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
      $form['test']['names_fieldset']['attachment'][$i] = array(
        '#type' =&gt; 'managed_file',
        '#title' =&gt; t('Name'),
      );
    }
    $form['test']['names_fieldset']['add_name'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Add one more'),
      '#submit' =&gt; array('agl_jira_example_add_more_add_one'),
      '#ajax' =&gt; array(
        'callback' =&gt; 'agl_jira_example_add_more_callback',
        'wrapper' =&gt; 'agl-jira-file-attach-wrapper',
      ),
    );
  }
  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'submit',
  );
  return $form;
}

function agl_jira_select_callback($form, &amp;$form_state) {
  $form_state['rebuild'] = TRUE;
  return $form['test']['names_fieldset'];
}

function agl_jira_example_add_more_callback($form, &amp;$form_state) {
  return $form['test']['names_fieldset']['attachment'];
}

function agl_jira_example_add_more_add_one($form, &amp;$form_state) {
  $form_state['num_names']++;
  $form_state['rebuild'] = TRUE;
}

function agl_jira_new_test_form_submit($form, &amp;$form_state) {
  $form_state['rebuild'] = TRUE;
}


removed $form['test']['names_fieldset'] = array(
    '#markup' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;&lt;/div&gt;',
  );, added reference to the form_state in agl_jira_example_add_more_callback..