Can I refresh a 'view' via AJAX after a form is submitted?

By: James Wyatt | Asked: 01/18/2024
ForumsCategory: General questionsCan I refresh a 'view' via AJAX after a form is submitted?
James WyattJames Wyatt asked 4 months ago
I have a page with a form at the top, and a listing view of that forms entries underneath. I want to be able to add a new entry in the form which will submit via AJAX, and then for the view to refresh via AJAX so that the new entry appears in the list of entries without the page refreshing. Is this possible, and how do I go about it? Thank-you in advance for your valued wisdom!
1 Answers
Best Answer
Victor Font answered 4 months ago
Yes, this is possible, but you have to know how to manipulate the HTML Document Object Model (DOM) with jQuery because you have to replace the content currently on the screen with new the content generated after the form submission. But how to do it could be tricky based on your confirmation action that you setup on the form. What confirmation action takes place? Are you displaying a success message or something else? Right now, with the information I have, I cannot tell you how to make this work. It may be possible to hook into the confirmation action, or it may not be possible and we have to figure an alternative. Whatever the case, it's a bespoke solution.
James WyattJames Wyatt replied 4 months ago

Thanks for your reply Victor - it's very helpful.

The form actions would be to display a success message, and send an email.

I guess I can use jQuery to trigger on the return of the success message and then call another ajax function to replace the contents of the 'view container' element.

Maybe the jQuery can call a PHP file that includes this function:

I'm not too familiar with jQuery but am aiming to learn more!

If you could let me know if I'm along the right lines or if you know of any functions that may be of use then that would be much appreciated.

Thanks again, James.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community