I have a simple custom form with 3 submit buttons - one that calls the default hook_form_submit and generates a table. The submit as the rebuild as TRUE:

$form_state['rebuild'] = TRUE;

that shows the complete form with the 3 buttons and the tables.

I need the 2 extra buttons to call different functions but I need inputs from the form itself or the table array.

At the moment, with the code I have, I can't access the form data and also, the tables created by the main submit disappear when I click the 2 extra buttons.

My code:

function MYMOUDLE_form($form, &$form_state) {
    $form['container']['submit'] = array(
        '#type' => 'submit',
        '#value' => t("Submit Form"),  

    $form['container']['export_1'] = array(
        '#type' => 'submit',
        '#value' => t("Export - 1"),
        '#submit' => array('MYMOUDLE_export_1'),

    $form['container']['export_2'] = array(
        '#type' => 'submit',
        '#value' => t("Export - 2"),
        '#submit' => array('MYMOUDLE_export_2'),

    return $form;

function MYMOUDLE_form_submit($form, &$form_state) {
    # do my stuff - generate tables

function MYMOUDLE_export_1($form, $form_values) {
    $someData1 = MYMODULE_handleFormData1($formValue1, $formValue2, $formValue3);
    drupal_set_message('Action for export 1 was done successfully.');

function MYMOUDLE_export_2($form, $form_values) {
    $someData2 = MYMODULE_handleFormData2($formValue1, $formValue2, $formValue3);
    drupal_set_message('Action for export 2 was done successfully.');
  • You probably need to rebuild the form, not redirect. See drupal.stackexchange.com/questions/224917/… – 4k4 Oct 17 '18 at 13:18
  • Sorry. I have redirect in my submit. $form_state['rebuild'] = TRUE; – zephirus Oct 17 '18 at 14:49
  • OK, if you rebuild the form you should see the form data when rebuilding the form. By the way you can check in $form_state the triggering element so that you know which button was pressed before rebuilding. – 4k4 Oct 17 '18 at 15:02
  • I added $form_state['rebuild'] = TRUE; to the MYMOUDLE_export_1 and MYMOUDLE_export_2 and changed the arguments to "$form, &$form_state". That solved it. I now have access to the form_state and the form is completely rebuilt. – zephirus Oct 17 '18 at 15:18

Rather than write submit function for each button use $form_state['triggering_element']. This will provide you the which button is clicked with form data in default hook_form_submit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.