0

I have 2 forms in my custom drupal7 module.
The 1st one (mymodule_form1_form) is a normal form with a lot of fields and a submit button at the bottom.
The 2nd one (mymodule_delete_data_form) has 1 hidden field and there is no submit button.
There is a link at the end of the 1st form which triggers a javascript logic. The javascript logic submits the 2nd form.

My problem is that I can't catch the submission of the 2nd form. The page reloads, but it doesn't entry into the mymodule_delete_data_form_submit function and I can't run the special business logic that removes the data from the database in this case.

The 1st form

function mymodule_form1_form($form, &$form_state, $node, $cid)
{
    $form['field1'] = array(
        '#type'       => 'textfield',
        '#title'      => 'Name',
        '#title_display'  => 'invisible',
        '#size'       => 60,
        '#maxlength'  => 128,
        '#required'   => TRUE,
        '#attributes' => array(
            'placeholder' => 'Name',
        )
    );

    $form['delete_record_link'] = array(
        '#markup'     =>
            "<div class='delete-record'>
                <a href='#' onclick='return false;'>I would like to delete my record...</a>
            </div>",
        '#attributes' => array(
            'class'   => array('additional')
        )
    );

    $form['submit'] = array(
        '#type'  => 'submit',
        '#title' => filter_xss("Submit"),
        '#value' => "Submit",
        '#title_display' => 'invisible'
    );

    return $form;
}

The 2nd form

function mymodule_delete_data_form($form, &$form_state, $node, $cid)
{
    $form['#submit'][] = 'mymodule_delete_data_form_submit';

    $form['delete_record'] = array(
        '#type' => 'textfield',
        '#title' => 'Remove the data from the database',
        '#title_display' => 'invisible',
        '#size' => 60,
        '#maxlength' => 128,
        '#attributes' => array(
            'placeholder' => 'Remove the data from the database',
        ),
        '#value' => "0"
    );

    return $form;
}

The submit function of the 1st form (it works like a charm):

function mymodule_form1_form_submit($form, &$form_state)
{
    echo "submit function 1<br/>";
    exit();
}

The submit function of the 2nd form (it doesn't trigger):

function mymodule_delete_data_form_submit($form, &$form_state)
{
    echo "submit function 2<br/>";
    exit();
}

The javascript logic that submits the 2nd form, when you click on the link in the 1st form:

jQuery(function() {
    var delete_record_field = jQuery("#edit-delete-record");
    var delete_record_link  = jQuery("#mymodule-form1-form .delete-record a");

    delete_record_link.on("click", function() {
        // set the value of the hidden field to 1
        delete_record_field.val("1");

        // submit the form
        jQuery("#mymodule-delete-data-form").submit();
    });
});

What is wrong in my code? How can I catch the submission of the 2nd form?

0

Drupal requires a submit button to submit - it uses the value of the submit button to determine which form was submitted.

Add a submit button to your form, and instead of executing $.submit() on your form, execute $.mousedown() on the hidden submit button.

1
  • thank you @Jaypan! This was the solution. I added a submit button to the 2nd form and I hid it by css, because this second form mustn't be visible. Apr 27 '17 at 15:24

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.