Score:0

AJAX callback not fired in hook_form_alter()

br flag

In Drupal 7, customer wants to add a button to the user_profile_form. So in my custom module, I create the button and its companion callback. But while the button appears on the form, I see no evidence the callback is being fired.

function member_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "user_profile_form") {
     
  $form['send_reset'] = array(
          '#type' => 'button',
          '#prefix' => "<div id='reset-div'>",
          '#suffix' => "</div>",
          '#value' => "Reset member",
          '#ajax' => [
            'callback' => 'member_reset_user',
            'wrapper' => 'reset-div',
            'event' => 'submit', //click, etc
          ],
          '#weight' => -150,
          '#attributes' => array(
            'id' => array('reset-cancel')
          ),
        );
}
function member_reset_user($form, &$form_state) {
  die();  
  try {
    my_user_function():
    watchdog('member', 'user reset', WATCHDOG_NOTICE);
  }
  catch (Exception $e) {
    watchdog('member', 'reset failed', WATCHDOG_ERROR);
  }
  return $form;
}

The html generated by Drupal suggests a submit function.

  <div id="reset-div"><input id="reset-cancel" type="submit" name="op" value="Reset member" class="form-submit"></div>

Since I'm not creating new form elements in my callback, just issuing a custom function, I cannot see what I'm doing wrong. I've been looking at this too long.

apaderno avatar
us flag
I take the AJAX callback isn't invoked as and when you expect. What did you do to be sure the callback is invoked?
apaderno avatar
us flag
(There is a typo, in that code, and a block of code isn't where it should be. They aren't the reasons for the callback not to be invoked, but the shown code isn't what you are actually using, probably.)
apaderno avatar
us flag
To make it clear, since the AJAX event isn't set for that button, the default one is *mousedown*. The AJAX callback is only invoked on that JavaScript event.
br flag
@apaderno Thx for responding. First line of callback has a die() to be certain. Revising goof above. The AJAX event threw me at first, but I reviewed and added event for click, submit. Still not triggering.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.