Stefanowitsch Posted January 9, 2022 Share Posted January 9, 2022 Hello! I want to make use of a JS modal that contains a form. Then the user submits the form I want to handle the submission via ajax and display a "success" message within the modal. I am wondering what is the best practice to handle ajax calls within processwire in general? Usually I would create a static PHP file that handles all the form validation which is then called via ajax after pressing the "submit" button. My question is: Where do I place such a static file within the PW folder structure? I want to create a custom "ajax" folder and place that file there: Then make the ajax request. This is a JS file that handles all functions for the site and sits somewhere within the "scripts" folder. $.ajax({ type: "POST", url: "/site/templates/ajax/form-submit.php", data: formData }).done(function (data) { console.log('form submitted'); console.log(data); }); This call results in a 403 (forbidden) error. The URL that is called looks like that: https://mywebsite/site/templates/ajax/form-submit.php So my question is: Do I need to create a custom template file (=form-submit) within the processwire backend first and then create a page with that template to make it accessible through an ajax request? Link to comment Share on other sites More sharing options...
kongondo Posted January 9, 2022 Share Posted January 9, 2022 (edited) Hi @Stefanowitsch, Very quick response. There is no best practice really. 9 minutes ago, Stefanowitsch said: This call results in a 403 (forbidden) error. ProcessWire is blocking access to form-submit.php because it is not a template file. There are different approaches to handling ajax requests, including: Creating a dedicated page (it doesn't have to be visible) and calling that Home page to handle all requests. You can then include your form-submit.php and let it deal with the response and send it back to your home template. Same page the form was called in (1-3 are similar). Using the new URL Hooks. Edited January 9, 2022 by kongondo 2 Link to comment Share on other sites More sharing options...
rick Posted January 9, 2022 Share Posted January 9, 2022 Howdy @Stefanowitsch, Have a look at this topic. Maybe it will help. 1 Link to comment Share on other sites More sharing options...
Stefanowitsch Posted January 10, 2022 Author Share Posted January 10, 2022 Thanks for your replies. I think I will stick to this solution: 1. Crate a Template for the Ajax Page 2. Create a Page with that template and call it via Ajax 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now