Frage

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?

War es hilfreich?

Lösung

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,
            );

Andere Tipps

As drupal doc states it, "#executes_submit_callback" won't prevent the page to send a POST request.

To do so, you have to add a custom attribute like:

#attributes' => array('onclick' => 'return (false);')
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top