[comments-module] Possible to make comment form submit via ajax
Created 6 months ago by emergingdzns

I'm building a mobile-first app and loading content for elements into a popup on the page. But if the user submits the comments the way it is, it reloads the page and the user loses their place. So, what I'm wanting to do is have it submit the form via ajax with a returned block of json or even html so I can insert it into the comments list on the page.

I'm having some kind of brain aneurysm because I can't wrap my head around how to do this. Should I just have my own controller and stuff the comment into the database or is there some method for adding using the comment module functionality that I'm just not seeing?

ryanthompson  —  6 months ago Best Answer

You can make the form ajax automatically by adding .setAjax(true) to the form string (you'll need to customize a wee bit).

Check out https://pyrocms.com/help/developer-tools/form-builders/defining-custom-ajax-forms-in-twig and the related links for some ideas on how to take it further.

The loading HTML back part would require you to override the handler as well most likely and utilize the returned data in your ajax JS (the default / demo ajax.js in the above material is a starter point for you to build on). Here's a direct link to that by the way (only auto-includes in the admin - public side you need to BYO script): https://github.com/anomalylabs/streams-platform/blob/1.3/resources/js/form/ajax.js

emergingdzns  —  6 months ago

That's exactly what I needed! Thanks @ryanthompson !