"Add More" button to form I'm trying to add an "add more" field to a form in my custom module. I've been looking at the examples module, with no luck. 

Here's what I have so far:

$form['#tree'] = TRUE;
  $form['names_fieldset'] = array(
  '#type' =&gt; 'fieldset',
  '#title' =&gt; t('People coming to the picnic'),
  // Set up the wrapper so that AJAX will be able to replace the fieldset.
  '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
);

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

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

$form['names_fieldset']['add_name'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; t('Add one more'),
  '#submit' =&gt; array('spower_add_more_add_one'),
  // See the examples in ajax_example.module for more details on the
  // properties of #ajax.
  '#ajax' =&gt; array(
    'callback' =&gt; 'spower_add_more_callback',
    'wrapper' =&gt; 'names-fieldset-wrapper',
  ),
);

if ($form_state['num_names'] &gt; 1) {
  $form['names_fieldset']['remove_name'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Remove one'),
    '#submit' =&gt; array('spower_add_more_remove_one'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'spower_add_more_callback',
      'wrapper' =&gt; 'names-fieldset-wrapper',
    ),
  );
}




function spower_theme($existing, $type, $theme, $path) {
  $base = array(
    'render element' =&gt; 'form',
  );

  return array(
    'spower_my_form' =&gt; $base + array(
      'template' =&gt; 'spower',
    ),
  );
}

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

/**
 * Submit handler for the "add-one-more" button.
 *
 * Increments the max counter and causes a rebuild.
 */
function spower_add_more_add_one($form, &amp;$form_state) {
  $form_state['num_names']++;
  $form_state['rebuild'] = TRUE;
}

/**
 * Submit handler for the "remove one" button.
 *
 * Decrements the max counter and causes a form rebuild.
 */
function spower_add_more_remove_one($form, &amp;$form_state) {
  if ($form_state['num_names'] &gt; 1) {
    $form_state['num_names']--;
  }
  $form_state['rebuild'] = TRUE;
}


The problem is that when I click "add more", the entire form is submitted. 

If the above isn't clear, I've added the module at http://pastebin.com/Hn7bkM2v and the spower_elements.inc at http://pastebin.com/CwHW5mSm.

Thanks for the help.
It's a well known problem of Drupal 7 - every form button is rendered with type="submit".

What you need to do, is to add prevent="click submit mousedown" to your button, to prevent actual submission. Maybe only one of those 3 is really needed, but this combination gave me pretty consistent results.

OK, but the above only prevents form from actual saving data. It still gets validated, and that's not exactly what you want, right? I already got answer on how to omit form's validation at AJAX button click.

Other approach would be to either add custom form element, let's call it real_button, a copy of button but with type="button", or to change type on document ready using jQuery.