SN_26
SN_26

Reputation: 205

Ajax button not functioning in newly loaded form content - Drupal 7

I have got a module, where, when a user clicks on a link, I am loading a new form in a dialog, which has an Ajax-enabled submit button. In my dialog, I don't want the entire drupal header and footer, etc., to display. All I am displaying is, the contents of that new form. So my form is outputted via json like this:

$json['html'] = drupal_render(drupal_get_form('my_form'));
drupal_json_output($json); 

Which works like I want, and only the form contents are displayed. However, when this happens, the submit button in that form, which is ajax-enabled, doesn't submit through ajax. Instead, it submits like a normal button and takes me to the same form on another page.

If I have the form being outputted and displayed normally, with the header and footers, like this:

$output = drupal_get_form('my_form');
return $output;

everything works just fine, and my submit button is 'ajax-enabled'.

I thought, maybe it was because I was loading the new content and the behaviors were not being attached to the these new form contents. So in my js file, I did this as well:

$('a.my_link', context).click(function() {
//processing done here
$('#my_modal_dialog').html(jsondata['html']);
}, "json");
Drupal.attachBehaviors($('#my_modal_dialog'));

That little addition, didn't fix the problem either. I am wondering as to why, if the form is outputted normally the button works as an ajax button, and when, only the form content is displayed, the ajax doesn't work.

Hoping someone will have the answer...Thanks in advance.

Upvotes: 0

Views: 2215

Answers (3)

SN_26
SN_26

Reputation: 205

I have got the problem solved and it can be found in the following link: drupal.org/node/1517414.

Upvotes: 1

kkatusic
kkatusic

Reputation: 92

Please check if $ replaced with jQuery, maybe this help.

Upvotes: 0

tsherif
tsherif

Reputation: 11710

I think the problem is indeed that the javascript isn't being applied to the new content, since it is run once after the page loads. Try using the live() method:

$('a.my_link', context).live("click", function() {
  ...
});

Note that the live() method seems to have been deprecated as of jQuery 1.7: http://api.jquery.com/live/

It seems to have been replaced by methods on or delegate.

Upvotes: 0

Related Questions