Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By IntoTheWoods on
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
Need to add a #name property
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.
Thanks. You saved me a few
Thanks. You saved me a few hours of debugging!!!
Alan Davison
Doesn't seem to work for me.
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:
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
Further Information
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:
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!
Thank you so much!
You also save my life right now! I was about to beat my PC against the floor ;-)