4

I am using Javascript/AJAX to call a path defined in a custom module which loads existing node content for display. i.e. you are at:

/custom/path

and it makes an AJAX request to /custom/content with arguments to load the content for the page (placed in #main-content). Content may be a page, video or other custom Content Types.

Custom navigation elements on the page cause further AJAX requests to /custom/content with different arguments to replace the content.

This has worked well until the need to incorporate webform's into the mix. Due to the non-refreshing nature of the page the webform needs to be loaded, displayed and submitted using AJAX.

I have setup hook_form_alter to add the necessary $form['actions']['submit']['#ajax'] to webforms and they work properly when called directly. The form submits via AJAX. However, when they are loaded via AJAX this does not work. They submit as per normal and navigate away from my desired wrapper page.

I have ensured Drupal.attachBehaviours is being called post AJAX load. I suspect my problem is in the loading/rendering of the webform in the module, but have not been able to successfully piece it together. The module serving /custom/content basically does the following:

$node = node_load($node_id);
$output = drupal_render(node_view($task, 'full', NULL));
echo $output;

It displays the webform perfectly (like other content) - it just doesn't implement the #ajax submission part. I've tried all manner of more specific form buulding for when $node-type = webform but no success.

  • What version of Drupal is this? – acouch Feb 11 '12 at 20:47
  • Did you ever solve this issue? Running into the same problem with AJAX loaded forms not being able to submit using AJAX. – sbrattla Jul 18 '12 at 12:02
  • Check if submit button if webform has class 'ajax-processed'. If not - then problem is that js-scripts that are required for ajax-submission of form are not loaded. – Eugene Fidelin Aug 3 '12 at 8:48
  • I ended up calling drupal_render($form) on the initial page (not those loaded with ajax) so that all javascript settings will be added to the page even though the form is not displayed. so when the form is loaded via AJAX, all javascript setting are already on this page. – Marius Ilie Aug 3 '12 at 10:59
  • Try this module drupal.org/project/ajax_links_api. If you create ajax links with this module, then it will automatically attach all ajax behaviours with ajax loaded content. – ARUN Nov 25 '13 at 14:55
0

Try to create ajax links with this module Ajaxify Drupal with JQuery Ajax.

It will load all ajax behaviour of loaded content(if loaded content contains ajax enabled form then that also)

Ajax links API module help you to load any content to a specified DIV or ajaxify whole drupal site, using JQuery Ajax. This module will also load JS for Ajax-loaded content by using Drupal.attachBehaviors(). In case you dont want to load js or css, you can alter tpl for path/node type.

0

You need to generate the JavaScript settings that Ajaxify the form, and pass them back with your response. You can do that like this:

$node = node_load($node_id);
$output = drupal_render(node_view($task, 'full', NULL));
$scripts = drupal_add_js();
if (!empty($scripts['settings']))
{
  $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
  $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
  $settings .= ');</script>';
}

echo $output . $settings;

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.