0

I want to load a form through Ajax in a custom Javascript Event as it is explained in this tutorial. I receive the form, but its ajaxified behaviour is very strange.

My Javascript:

var address = $(location).attr('protocol') + '//' + $(location).attr('hostname');
var url_get_button = address + 'mymodule/get_button/';

Drupal.ajax.prototype.specifiedResponse = function() {
  var ajax = this;
  try {
    $.ajax(ajax.options);
  }
  catch (err) {
    return false;
  }
  return false;
};

var ajax_settings = {};
ajax_settings.event = 'onload';
ajax_settings.keypress = false;
ajax_settings.prevent = false;
ajax_settings.url = url_get_button;
Drupal.ajax['get_button'] = new Drupal.ajax(
  null, $(document.body), ajax_settings);

//the following is somewhere at the right js event...
Drupal.ajax['get_button'].specifiedResponse();

My php code:

function mymodule_menu() {
  $items['mymodule/get_button'] = array(
    'page callback' => 'mymodule_send_button',
    'type' => MENU_CALLBACK,
    'access callback' => 'user_is_logged_in',
  );
  $items['mymodule/next_ajax'] = array(
    'page callback' => 'mymodule_next',
    'type' => MENU_CALLBACK,
    'access callback' => 'user_is_logged_in',
  );
}

function mymodule_get_form ($form, &$form_state) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#ajax' => array(
      'event' => 'click',
      'path' => 'mymodule/next_ajax/',
      'effect' => 'fade',
    ),
  );
  return $form;  
}

function mymodule_send_button() {
  $form = drupal_get_form('mymodule_get_form');
  $form = ajax_pre_render_element($form);

  $commands = array();
  $commands[] = ajax_command_replace('#replace-this', render($form));
  $commands[] = ajax_command_invoke(NULL, "some_js");

  $button = array('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($button);
}

function mymodule_next() {
  commands = array();
  $commands[] = ajax_command_alert('works!');
  $message = array('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($message);
}

function mymodule_node_view() {
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_library('system', 'jquery.form');
}

Well, if Drupal.ajax['get_button'].specifiedResponse();is called in the Javascript event, the button will be loaded, but if I click on the button, the page is reloaded with url_get_button (which was used to request the button) instead of getting the alert window with 'works!'.

But if I add another form builder function and call this function with drupal_get_form() within the hook_node_view() (without returning anything!!), it will work.

So I add:

function mymodule_get_another_form ($form, &$form_state) {
  //I have to add the following line in order that it works
  $form2['submit'] = array();
  return $form2;
}

and change my hook_node_view() to:

function mymodule_node_view() {
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_library('system', 'jquery.form');
  $form2 = drupal_get_form('mymodule_get_another_form');
  //I don't return $form2...
}

Well, in summary: I want to request an ajaxified button through a custom Javascript event. I receive the button, but it is not ajaxified and on top the wrong url is linked. But if i call drupal_get_form() with another 'empty' form builder function during the page load, it will work and I will get the alert message after clicking on the button.

I use Drupal 7.

Does anyone have an advice for me? Why isn't it working without the second form builder function and why does it work with it?

I would welcome every advice. Thank's a lot.

Daniel

0

I got it: In a sidebar, there was another form with a submit button provided by another module. This submit button has also the key 'submit'. In the form builder function I changed the key 'submit' to e.g. 'mymodule_submit' and now it works! So I don't need this strange second form builder function anymore.

  • @Wtower well, OP need to wait for 48 hours (I guess) to accept their own answer. ;) – Krishna Mohan Nov 12 '14 at 14:38

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.