Simple AJAX Comment Preview in Drupal 6

As I said in my previous post, I’m working on upgrading Computer Science in Syria website to Drupal 6.

I wanted also to have AJAX comment preview in nodes, but I wanted it to be simple without lots of JavaScript loaded. So as usual I got pissed off and decided to create my own code to do it. 8)

I used Simple Modal by Eric Martin to display the preview in a model instead of a new page.

The code is easy too like the previous post’s code, I edited also the drupal.js file and added the following code:

The process is too simple:

  1. @line 01: I check to see if the current page is a node page, I just want to make the preview code working on node page.
  2. @line 03: I show the user a modal with a loading image Loading ...
  3. @lines 04~09: I check the editor, if it is enabled I disable it to get the HTML from the comment. If it is not enabled I get the HTML directly without disabling it.
  4. @lines 10~18: I gather the data of the form and post them.
  5. @line 20: I remove the loading image.
  6. @line 21: I append the HTML data from the response to the modal content.
  7. @lines 22~23: I set the form hidden values (form_build_id and form_token) to the values from the response so I don’t get a Validation Error.
  8. @line 26: I return false so the browser doesn’t post the data again to the new page.

Here is an example:

Simple 😉

2 thoughts on “Simple AJAX Comment Preview in Drupal 6

  1. Cool. Found this post when googling for AJAX comments in Drupal. Might try it as an alternative to the AJAX Comments module.

    1. Thanks 🙂
      You can try it, and might decide on using it.
      Anyway it needs more testing, for example when you have CAPTCHA module installed, this script won’t send the CAPTCHA result to the server and so the server will not respond properly.

      I might work on it and enhance it when I have free time, but I encourage you to do so if you’re a developer since it’s open source 😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.