ajaxdrupal-7drupal-hooks

Adding Button to Webform - Issue: Doing Submit, not Ajax callback


I am adding a button to a webform using hook_form_alter:

$form['submit_ajaxSearch'] = array(
                '#type' => 'button',
                '#ajax' => array(
                    'action' => 'click',
                    'callback' => 'search_callback',
                    'wrapper' => 'confirm',
                    'method' => 'replace',
                    'name' => 'search',
                ),
                '#value' => t('Address Lookup'),
            );

I can setup a jQuery .click() in the module, but can't get the Ajax callback to execute. It works when the button is being added to a form as part of the module (i.e. if it was mymodule_form ), but when added to a webform in mymodule_form_alter it is executing a submit instead of the callback.

How can I get the ajax callback to execute the Ajax, not Submit?


Solution

  • Since Drupal creates a "Submit" button even though you only wanted a simple button, you will have to specify to drupal that you don't want the button to execute the submit callback. You can do this by setting "#executes_submit_callback" to false for that button.

    eg:

               $form['submit_ajaxSearch'] = array(
                    '#type' => 'button',
                    '#ajax' => array(
                        'action' => 'click',
                        'callback' => 'search_callback',
                        'wrapper' => 'confirm',
                        'method' => 'replace',
                        'name' => 'search',
                    ),
                    '#value' => t('Address Lookup'),
                    '#executes_submit_callback' => FALSE,
                );