Jump to content

Add comments without page refresh


douglas81
 Share

Recommended Posts

The current default for the Comments module seems to be...

1.) User enters name, email and comments

2.) User hits submit

3.) Page is refreshed

4.) Thank-you message or error message returned

How do we avoid step 3, the page refresh? Is it possible with the standard commenting system?

Some sort of AJAX must be involved, but I don't know if it's possible to modify the current comments system to accommodate this.

Any pointers much appreciated.

Link to comment
Share on other sites

Just take a look a the form. cite, email, website, text & submit are the visible fields. Maybe there are some hidden ones, too. To submit a comment just send all those datapoints back to the current site as POST data. The easiest way to have this done by default would be a hooking module with a new javascript file. The hook would then append this js file on something like CommentForm::render(). But you would need to include $config->scripts, in your frontend templates. 

But keep in mind, that it's not only about submiting a comment. Normally this also includes displaying the new comment, which may in itself have consequences for some kind of pagination. So there's a little bit to think about.

  • Like 1
Link to comment
Share on other sites

Yeah, it's quite involved. I'm afraid a lot of what you said doesn't quite make sense to me you. I'm still getting my head around hooks and making my own modules in PW (I've done a couple, but it's always a bit "black magic" for me at the moment).

What I was thinking, is to use JS to grab all the relevant stuff from the form, POST it using AJAX to my own custom PHP. This PHP will take the POST data, analyse it, add the comment if everything is okay. It will return a success or failure message. Then, again using JS, update a DIV with the returned message.

Of course, this doesn't account for adding the comment. If it was automatically approved without moderation, I could always just update the relevant DIV, again with JS.

Link to comment
Share on other sites

The module part I mentioned would onyl be there to add your extra javascript automatically, but that's kinda unimportant in the beginning. For the php part you could leverage the processInput() function of CommentForm.php. I'm currently just not sure how to correctly call it, so that it's adding to the right page / comments field. Maybe someone else could help you there. But mostly you'd need to take care of the AJAX stuff by yourself.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...