I am using the WordPress Plugin Boilerplate to show a form to the user via shortcode. I am just a little confused of the flow of data not so much the syntax to do it.
My Main Public Class has a method that creates a form when a user adds the shortcode. I have a class that can take the form data and build a URL that calls an API.
This is how I thought it should work: 1. Javascript function fires when "Submit" is clicked. It checks to make sure the form is complete if so use AJAX to pass the data to my Processesing script. 2. The script builds the URL and calls the API and data should be returned.
Question: Once the data is returned can I pass the data and update the original page with the form to show the data? This is were I am not 100% sure how to do.
So the user experience should include completing the form, submitting (maybe a loader animation) and the data appears below the form.
****I don't need code I am just looking for an direction on the flow of data.**
I am using the WordPress Plugin Boilerplate to show a form to the user via shortcode. I am just a little confused of the flow of data not so much the syntax to do it.
My Main Public Class has a method that creates a form when a user adds the shortcode. I have a class that can take the form data and build a URL that calls an API.
This is how I thought it should work: 1. Javascript function fires when "Submit" is clicked. It checks to make sure the form is complete if so use AJAX to pass the data to my Processesing script. 2. The script builds the URL and calls the API and data should be returned.
Question: Once the data is returned can I pass the data and update the original page with the form to show the data? This is were I am not 100% sure how to do.
So the user experience should include completing the form, submitting (maybe a loader animation) and the data appears below the form.
****I don't need code I am just looking for an direction on the flow of data.**
Share Improve this question asked Jun 10, 2019 at 22:39 DenoteoneDenoteone 2091 gold badge4 silver badges12 bronze badges 2- Yes, you can. You can submit arbitrary data via AJAX and update the page after receiving a response from the server. But maybe you should post your code, if you need further help. – Sally CJ Commented Jun 11, 2019 at 0:30
- 1 Thank you for confirming and will provide code once i try and get some results on my own. Thanks! – Denoteone Commented Jun 11, 2019 at 5:54
1 Answer
Reset to default 0Take a look at the codex for AJAX in Plugins. It provides example of how WordPress expects AJAX requests are made and how to return information back to the request in javascript.
The request gets routed through admin-ajax.php
which locates and action in PHP. The rest is up to you.
Note: you must echo/print the content for it to be returned in the body of the response. It's helpful for it to be encoded with wp_json_encode()
as well.