When using the #ajax form API, am I correct in thinking that foo_submit() should not be called when the #ajax callback completes? Right now Drupal seems to be calling foo_submit() during the #ajax callback. Here's my form button definition:

$form["migrate"]["start"] = array(
  "#type" => "submit",
  "#value" => "Start",
  "#ajax" => array(
    "callback" => "migrate_scan",
    "wrapper" => "migrate-work",
    "progress" => array(
      "type" => "bar",
      "message" => "Starting...",
      "url" => url("migrate/progress"),
      "interval" => 750,
    ),
  ),
);

The function accounts_migrate_scan() returns a normal $form array. But, included in the replacement content is a drupal_set_message() that I create in foo_submit() which means that the foo_submit() function is also being called during the calls to migrate_scan(). So, I get the output of accounts_migrate_scan() with a drupal_set_message() prepended to it.

What am I missing here? I thought that a successful call to the #ajax callback ought to prevent any call to foo_submit(). Am I not understanding how this is supposed to work?

Comments

tjg’s picture

I found this page:

http://api.drupal.org/api/examples/ajax_example!ajax_example_graceful_de...

and I re-structured my code to follow that example, but now when I get to the end of step 2, the "next" button is disabled - clicking it has no effect at all.

tjg’s picture

As it turns out, the button is not disabled. If I attach an "onclick" handler through the Forms API, the "onclick" event fires, but the button does not initialize its AJAX callback. Here's the button definition for the form data that gets returned at the end of step 2:

$form["start"] = array(
  "#type" => "submit",
  "#value" => "Start Migration",
  "#attributes" => array(
    "onclick" => "return confirm('Are you sure you want to migrate? This cannot be un-done!')",
  ),
  "#ajax" => array(
    "callback" => "migrate_start",
    "wrapper" => "work-container",
  ),
);
nodiscipline’s picture

I am also having the same:$ I can see that the ajax call is done in firebug. Yet in the Post is no mention of the callback function. The reply through ajax has nothing to do with what my callback stats. My form looks like this :

$form['Notifications']['update_notifications_button'] = array(
    '#type' => 'button',
    '#value' => t('Done, now tell us about you.'),
    '#limit_validation_errors' => array(),
    '#name' => 'update_notifications_button',
    '#attributes' => array(
      'style' => 'display: none;',
    ),
    '#ajax' => array (
      'callback' => 'processmaker_notifications_update',
      'event' => 'change',
      'progress' => array(
        'type' => 'none',
      ),
    ),
  );

The change is triggert through javascript and I can see that the ajax call is done when ever it should. The callback is this:

function processmaker_notifications_update ($type, $arguments) {
  $commands = array();
  $commands[] = ajax_command_alert('result!');
  $drupal_ajax = array ('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($drupal_ajax);
}

ajax returns:

[{"command":"settings","settings":{"basePath":"\/drupal7\/","pathPrefix":"","ajaxPageState":{"theme":"bartik","theme_token":"p7NYUs8gsoo4ZzTU5Y8f0LSd6wpwhP02tkwBlBLfTsk"},"overlay":{"paths":{"admin":"node\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\noverlay\/dismiss-message\nuser\/*\/shortcuts\nadmin\nadmin\/*\nbatch\ntaxonomy\/term\/*\/edit\nuser\/*\/cancel\nuser\/*\/edit\nuser\/*\/edit\/*","non_admin":"admin\/structure\/block\/demo\/*\nadmin\/reports\/status\/php"},"pathPrefixes":[],"ajaxCallback":"overlay-ajax"}},"merge":true}]

Any Ideas?

tjg’s picture

I've made a bit of progress:

To get the foo_submit() hook not to run, I've switched the buttons from "#type" => "submit" to "#type" => "button"

But I'm still stuck on the second submit button not working:

The button that appears during step 2 of the submission process still does not fire off the AJAX callback when clicked. Nothing at all happens. No HTTP request is made from the browser to the server. There are no JavaScript errors in the JavaScript console. The onClick #attribute I've added does get fired, but nothing else. As a reminder, the second button is defined during the first callback as:

$form["start"] = array(
  "#type" => "button",
  "#value" => "Start Migration",
  "#attributes" => array(
    "onclick" => "return confirm('Are you sure you want to migrate? This cannot be un-done!')",
  ),
  "#ajax" => array(
    "callback" => "migrate_start",
    "wrapper" => "work-container",
  ),
);
Anandkumar’s picture

+1
I am experiencing the same problem where the ajax event is triggered but the ajax callback is not called on the second time.

Shabana Blackborder’s picture

You should have a unique '#name' defined for each ajax button. Please try that and see if things work.

Shabana Navas
Drupal Developer
http://blackborder.co.uk

phreestilr’s picture

I think I'm having the same problem. The ajax call is made, the response is received, but the form is not replaced. I gave the buttons '#name' values, but that doesn't help. The weird thing is the ajax multi-step form works perfectly for user 1, but not for any other user.

NEVERMIND
I found my problem. I just had some bad javascript that was screwing it up.

ajay25487’s picture

our name is unique.
can you tell us some example..

ajay