I can’t get my head around calling an AJAX-request to prevent page refresh in a form.
I have read through the forum and here and there are examples given but none fits my situation I think.
I started of without AJAX like this:
I have a snippet which contains a contact form. This snippet I use on various pages of a multilingual website.
Because I use this snippet on various pages, I use the site.php controller for handling the submit-data
This worked so far and now I wanted to implement an ajax-request to stop the page refresh.
The new snippet contains a script at the end like the following but I don’t know what url I have to pass.
Do I have to pass site.php? But how? Do I need routes?
Or can I use an individual controller for my snippet? But how would I call it?
Actually I don’t exactly know. I have read so many posts here in the forum from 2015 till now and might have messed up some stuff.
To short it down. I just want to find an ajax solution to my form because of the page refresh issue. And as I use this as a snippet on various templates, I couldn’t go for the standard template/controller structure.
During my experiments I managed to get something to work with a route in the config.php but I didn’t want to put all the data logic in the config.php as it overloads the place in my opinion.
I use a self-programmed captcha, the value of which would be renewed after every reload, which is not desirable. Google for “Quform - Responsive Ajax Contact Form”. I use it successfully in all my projects. However, it is a very extensive tool that requires training time and PHP knowledge.
I’m also looking for a more compact tool, but couldn’t find anything convincing.
Thank you for mentioning this but I expect that the right hint will help me to solve it without learning a newtool. As @texnixe already mentioned, I think I have just confused some logic here.
Stick with vanilla JS and use the Fetch API for your Ajax request (and remove all this other $-jquery stuff). If you don’t have the jQuery library loaded (and imo you shouldn’t), then jQuery code will not work.
This recipe has an example fetch request: Load more with Ajax | Kirby CMS. Totally different example, though with only a GET request, so you need to modify this to post the data instead.