Ajax-ifying Drupal Forms
Thu, May 21, 2009 by Scott
I always get a really good sense of accomplishment when I write a lot of code and it does what I want. I sit back and breath a sigh of relief until someone shows me that I could have done the same thing with 1 line of code. Surely, one line of code can't accomplish 'everything' I want, right? Well, almost.
Included with Drupal is jQuery...obviously...but not so obvious is a jquery function called ajaxForm. The task at hand was to ajaxify the Drupal comments for a Video content type, so that you could comment on the video without the page refreshing and interrupting video playback.
I started writing some custom jQuery, collecting all the form field values into a data array, and using $.post to send the data to a url, which I then created a page callback for using Drupal's menu system.
$('#comment-form input#edit-submit').bind("click", function(e) {
var title = $('input#edit-subject').val();
var body = $('textarea#edit-comment').val();
var wall = $('select#edit-friends option').val();
var star = $('select#edit-vote-1 option').val();
var data = {
'title' :title,
'body' :body,
'wall' :wall,
'star' :star
}
$.post('/video/comment', data, function(data) {
alert(data);
});
return false;
})
No problems so far...the form successfully posts to the url and I have all the data to work with now. But how do I mimic the comment submit? I don't want to write a bunch of SQL...I'm just a designer!
Enter ajaxForm. It gathers all the information from the form elements and submits the form, all automatically! All you have to do is this:
$('#comment-form').ajaxForm();
For more information about usage, and options, see:
http://malsup.com/jquery/form/
betsson posted on October 5, 2009 10:40 pm
Thanks for the code tutorial...
Manuel Garcia posted on June 21, 2009 3:40 pm
you could also use the http://drupal.org/project/ajax_comments module for this purpose, works great!
boediger posted on June 16, 2009 3:15 pm
Included with Drupal is jQuery...obviously...but not so obvious is a jquery function called ajaxForm. The task at hand was to ajaxify the Drupal comments for a Video content type, so that you could comment on the video without the page refreshing and interrupting video playback.
boediger
IceCreamYou posted on May 30, 2009 3:12 am
Yep, it's the image module: http://drupal.org/project/image
Anonymous posted on May 24, 2009 4:39 pm
That looks like the image module.
admin posted on May 21, 2009 9:08 pm
Another good option thanks for sharing! Using the AHAH capabilities though doesn't give way to many options for working with the response data...like appending the new comment html to the comment list. You'll need an external JS file anyways!
On an unrelated note, what are you using for your image gallery on your website? I'm looking for a nice gallery module that uses images as nodes.
IceCreamYou posted on May 21, 2009 12:53 am
Drupal can automatically do this for you by using hook_form_alter() to add the #ahah property to the submit button:
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...
This is definitely a useful application of JavaScript-driven comments though.
Post new comment