How to add a button "Add More" with ajax for adding multiple fields? I want to add a button "Add More" with ajax for adding multiple fields
I have writed this code, I don't understand why when I click to "add more" nothing work, there is an solution?

this is my code:

function produits_add($form, &amp;$form_submit){

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

        // Create wrapper for ajax
        $form['#prefix'] = '&lt;div id="form-wrapper"&gt;';
        $form['#suffix'] = '&lt;/div&gt;';

        for ($i = 0; $i &lt; $form_state['num_names']; $i++) {

            $form['name_' . $i] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('Name')

            );
        }

        $form['add_button']= array(
            '#type' =&gt; 'button',
            '#value'=&gt; 'Add more',
            '#ajax' =&gt; array(
                'callback' =&gt; 'ajax_add_col',
                'method' =&gt; 'replace',
                'effect' =&gt; 'fade',
                'wrapper' =&gt; 'form-wrapper',
            ),
        );




        $form['submit'] = array(
            '#type' =&gt; 'submit',
            '#value' =&gt; 'Submit',
            '#submit' =&gt; array('produits_add_submit'),
        );





        return $form;
    }

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

        $form_state['num_names']++;

        // rebuild whole form with new values
        $form_state['rebuild'] = true;
    }

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

// Some code
}
In your form function, group the textfields under a key, you can define your own key. I use group here.

// Create wrapper for ajax
$form['group']['#prefix'] = '&lt;div id="form-wrapper"&gt;';
$form['group']['#suffix'] = '&lt;/div&gt;';

for ($i = 0; $i &lt; $form_state['num_names']; $i++) {

  $form['group']['name_' . $i] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Name')
  );
}


In your ajax callback, return part of the form.

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

  $form_state['num_names']++;

  // rebuild whole form with new values
  $form_state['rebuild'] = true;

  return $form['group'];
}