AJAX form default values I'm using an AJAX form to match multiple values of a field to an other entity, to do so i've implemented the example we have in the ajax_example ( https://api.drupal.org/api/examples/ajax_example!ajax_example_graceful_degradation.inc/group/ajax_degradation_example/7 ), see ajax_example_add_more*

It is working great (except i would prefer being able to remove the field i want and not always the last one but we can't have everything).

When i submit the form, my chosen values are saved in database and the page is refreshed. The problem is that the form became empty, i might have multiple names for one fieldset and now i only have one empty textfield.

If we continue with the ajax_example, what i can do is this :

$myValues = variable_get('my_var', '');


And on this part add a default_value (let's assume that in my $myValues['name'] there is the list of the names)

$form['names_fieldset']['name'][$i] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Name'),
      '#default_value' =&gt; $myValues['name'],
);


In the case where i add only one name, it will work. But if i have multiple name, it will only display the last one.

Do you know how can i do to recreate all the form as it is store in the db ?

Thanks by advance

[EDIT]

Thanks to sareed, i found the way to do so. I'll show the modification to ajax_example_add_more. In the example there is only one fieldset, in my work there is x fieldset so i will add $fieldsetName to know which one :

Change :

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


By

     $myValues = variable_get('my_var', '');
     if (empty($form_state[$fieldsetName]['num_names'])) {

          if(isset($myValues[$fieldsetName])) {
            $form_state[$fieldsetName]['num_names'] = 
              count($myValues[$fieldsetName]) &gt; 0 ? count($myValues[$fieldsetName]) : 1;
          }
          else {
            $form_state[$fieldsetName]['num_names'] = 1;
          }
      }


AND

  for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
    $form['names_fieldset']['name'][$i] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Name'),
    );
  }


TO

  $form['names_fieldset']['name'][$i] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Name'),
    '#default_value' =&gt; isset($myValues[$fieldsetName][$i]) ? $myValues[$fieldsetName][$i] : '',
  );
Loop through the myValues (only use the foreach if you are not already in a for loop) and use the appropriate array element:

foreach($myValues as $index =&gt; $name) {
  $form['names_fieldset']['name'][$index] = array( // Already using $i here. Are you in a for loop?
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Name'),
    '#default_value' =&gt; $name, // Or $myValues[$i]['name'] if you are already in a for loop (it looks like you might be).
  );
}


Depending on the structure of the array you may have to change this a little. If these don't work and you can't figure it out, add a print out of one of your smaller arrays using print_r to your question and we can help.