Autofill texfields based on select option in dynamic fieldset In my custom form i am dynamically generating multiple fieldsets.Each field set has select option(dropdownlist) and textfield generated dynamically. The value for textfield is dynamically generated based on user selected value from select option. 


My problem is that if i select species from fieldset2 then density texfield1 in fieldset1 is overwritten and density textfield2 in fieldset2 is empty which is wrong. Each time the ajax callback return value is overwriting textfield1 in fieldset1(Field#1)

function fuel_design_tool($form, &amp;$form_state) {
$form = array();
$form['#tree'] = TRUE;

if (empty($form_state[FIELDS_NUM])) {
    $form_state[FIELDS_NUM] = 1;
}

for ($i = 1; $i &lt;= $form_state[FIELDS_NUM]; $i++) {

    $form['field'][$i] = array(
        '#type' =&gt; 'fieldset',
        '#title' =&gt; t('Field #@num', array('@num' =&gt; $i)),
        '#collapsible' =&gt; TRUE,
        '#collapsed' =&gt; FALSE,

    );        

    $form['field'][$i][FORM_FIELD_SPECIES] = array(
        '#type' =&gt; 'select',
        '#required' =&gt; TRUE,
        '#title' =&gt; 'Choose Species',
        '#options' =&gt;  _load_species1(),
        '#default_value' =&gt; 0,
        '#ajax' =&gt; array(
             'callback' =&gt; 'fuel_design_tool_callback',
             'wrapper' =&gt; 'fuel_design_tool-wrapper',
             'event' =&gt; 'change',
           )

       );

      $profile = isset($form_state['values']['field'][$i]['FORM_FIELD_SPECIES']) ? ($form_state['values']['field'][$i]['FORM_FIELD_SPECIES']) : NULL;
    $form['field'][$i][FORM_FIELD_DENSITY] = array(
        '#type' =&gt; 'textfield',
        '#prefix' =&gt; '&lt;div id="fuel_design_tool-wrapper"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',
        '#title' =&gt; t('Density'),
        '#value' =&gt;$profile,

    );
    $form['add_field'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add More fieldet'),
    '#submit' =&gt; array('fuel_design_tool_add_name')
    ),

     $form['submit_button'] = array(
     '#type' =&gt; 'submit',
     '#value' =&gt; t('Submit'),
     "#submit" =&gt; array("fuel_design_tool_submit"),
    );

     return $form;
 }

 function fuel_design_tool_add_name($form, &amp;$form_state) {

    $form_state[FIELDS_NUM]++;
    $form_state['rebuild'] = TRUE;
   }

function fuel_design_tool_callback($form, &amp;$form_state)
 {
   for ($i = 1; $i &lt;= $form_state[FIELDS_NUM]; $i++)
   {

  $form['values']['field'][$i]['FORM_FIELD_DENSITY'] = $form_state['values']['field'][$i]['FORM_FIELD_SPECIES'];
  return array (
    $form['field'][$i]['FORM_FIELD_DENSITY'],

   );
  }

 }
 }


My callback function returns only one value and that by default filled to fiedset#1 textfield.I guess i need to modify my callback function.

I am looking forward for your kind help. 

If my question is not clear please let me know. I am finding bit difficult to explain this problem
Give the textfield wrappers unique ids, e.g. fuel_design_tool-wrapper-1 and fuel_design_tool-wrapper-2, and assign accordingly in the FAPI array.