Hi,
I'm creating a dynamic multistep form with a different edit button for each of many rows in a table, all of which have the same value ('edit'), but I find that whatever button I click, I end up with the parents of the final button on the form, so I can only end up editing the last row in the table! I've attached a very simple example below which has the same behavior. Renaming the buttons is not an option. Help???

function testsubmit_menu() {
  $items = array();
  $items['testsubmit'] = array(
    'title' => t('Submit Test Form'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('testsubmit_form'),
    'access arguments' => array('access content'),
  );
   
  return $items;   
}

function testsubmit_form(&$form_state) {
  $form['a'] = array(
    '#tree' => TRUE,
    '#type' => 'fieldset',
  );
  $form['a']['edit'] = array(
    '#tree' => TRUE,
    '#type' => 'submit',
    '#submit' => array('testsubmit_submit_handler'),
    '#value' => t('Edit'),
  );
  $form['b'] = array(
    '#tree' => TRUE,
    '#type' => 'fieldset',
  );
  $form['b']['edit'] = array(
    '#tree' => TRUE,
    '#type' => 'submit',
    '#submit' => array('testsubmit_submit_handler'),
    '#value' => t('Edit'),
  );
  return $form;
}

function testsubmit_submit_handler($form, &$form_state) {

  //print_r($form_state['clicked_button']);
  drupal_set_message('#parents: '.$form_state['clicked_button']['#parents'][0]);
  drupal_set_message('#array_parents: '.$form_state['clicked_button']['#array_parents'][0]);
}

Comments

milesw’s picture

I just ran into this exact situation. On one page I had a single form with 4 submit buttons, each with the same #value. The clicked_button was always wrong. Once I set #name on the submit buttons to something unique it worked itself out.

Alan D.’s picture

Thanks. You saved me a few hours of debugging!!!

tom.pauwaert’s picture

Hi,

I seem to be having the same problem as the OP. However I tried to set the #name property of my submit buttons but on printing with dsm
the name always seemed to remain 'op'.
What I am basically doing, and if I understand your post correctly, this is also what you had just described:

$form['alter'][school]['add_departments'.$id] = array(
	'#type' => 'submit',
	'#name' => $var1.$var2,
	'#value' => 'Add Department',
	'#submit' => array(
		myform_validate,
		myform_submit,
	),
);

Does there seem to be something obvious I'm doing wrong or have I misunderstood what you meant in some way ?

Thanks in advance,
Tom

laceysanderson’s picture

So when you submit the form by pressing the above button where $var1.$var2 evaluates to 53 (for example).
$form_state['values'][53] = 'Add Department' and
$form_state['clicked_button']['#name'] = 53

Therefore, in your submit you could have:

if (preg_match('/Add Department/', $form_state['values'][ $form_state['clicked_button']['#name'] ])) {
  switch ($form_state['clicked_button']['#name']) {
    case 53:
      do something....
    break;
  }
}

Note: $form_state['values']['op'] is replaced by $form_state['values'][53]. If you are using this method be Very careful of using $form_state['values']['op'] because if you pressed the Add Department button that would evaluate to NULL...

Hope this helps!