Home › Forums › Front-end Issues › Saving acf_form() via AJAX – maybe it can be done?
So I have an acf_form() I want to save via AJAX. I’ve looked around online and there doesn’t seem to be much on this subject, or else, I’m an awful google searcher.
So here is what I’m thinking.
acf_form() needs acf_form_head() to save data from $_POST to acf fields.
So I figure, if I add these hooks and localization:
add_action( 'wp_ajax_nopriv_acf_form_head', 'acf_form_head' );
add_action( 'wp_ajax_acf_form_head', 'acf_form_head' );
wp_localize_script( 'my-script', 'acf_form_head', array( 'ajaxurl' => admin_url( 'admin-ajax.php', 'http' ) ) );
And if I am able to get javascript to format the names and val() of the acf_form() inputs as acf_form_head() is expecting, [something like this]
= Array
(
[_acfnonce] => f0c5e0fd02
[_acfchanged] => 0
[_acf_form] => eyJpZCI6ImRhdGEtc3Vic2VjdGlvbiIsInBvc3RfaWQiOjIwOTUsIm5ld19wb3N0IjpmYWxzZSwiZmllbGRfZ3JvdXBzIjpmYWxzZSwiZmllbGRzIjpbImZpZWxkXzU1YjEyMDhhNDBiMGUiLCJmaWVsZF81NTk1YzFmNGEzZTNhIiwiZmllbGRfNTU5NWMyMGFhM2UzYiIsImZpZWxkXzU1OTVjMjZmYTNlM2MiXSwicG9zdF90aXRsZSI6ZmFsc2UsInBvc3RfY29udGVudCI6ZmFsc2UsImZvcm0iOnRydWUsImZvcm1fYXR0cmlidXRlcyI6eyJpZCI6InBvc3QiLCJjbGFzcyI6IiBhY2YtZm9ybSIsImFjdGlvbiI6IiIsIm1ldGhvZCI6InBvc3QifSwicmV0dXJuIjoiaHR0cDpcL1wvYW1lcmlmbHV4LmRldlwvc2l0ZXNcL2NvcmUtc2l0ZS1tYW5hZ2VtZW50XC9jb3JlLXN1Ym1pc3Npb24tZm9ybVwvP2NvcmU9MjU3NDIwZmU0MmQzN2JmYTk4NjMwNmYxY2U3YjI5Y2UmdXBkYXRlZD10cnVlIiwiaHRtbF9iZWZvcmVfZmllbGRzIjoiIiwiaHRtbF9hZnRlcl9maWVsZHMiOiIiLCJzdWJtaXRfdmFsdWUiOiJTYXZlIiwidXBkYXRlZF9tZXNzYWdlIjoiIiwibGFiZWxfcGxhY2VtZW50IjoidG9wIiwiaW5zdHJ1Y3Rpb25fcGxhY2VtZW50IjoibGFiZWwiLCJmaWVsZF9lbCI6ImRpdiIsInVwbG9hZGVyIjoid3AifQ==
[acf] => Array
(
[field_55b1208a40b0e] => Array
(
[0] => Array
(
[field_55b1208a40b10] => Yes
[field_55b1208a40b11] => Yes
[field_55b1208a40b12] => Yes
)
[1] => Array
(
[field_55b1208a40b10] => Select
[field_55b1208a40b11] => Select
[field_55b1208a40b12] => Select
)
[2] => Array
(
[field_55b1208a40b10] => Select
[field_55b1208a40b11] => Select
[field_55b1208a40b12] => Select
)
[3] => Array
(
[field_55b1208a40b10] => Select
[field_55b1208a40b11] => Select
[field_55b1208a40b12] => Select
)
)
[field_5595c1f4a3e3a] => asdf asd fasd asd asd
[field_5595c20aa3e3b] => a dsfa dsf asdf
[field_5595c26fa3e3c] => asdf asdf
)
)
Then it maybe possible to use AJAX to save an acf_form.
Anyone else tried this, or something similar?
Thanks
Seems to be the start of a solution here http://support.advancedcustomfields.com/forums/topic/frontend-form-post-via-ajax/
This one seems to have more information and even some helpful guidance from our benefactor. http://support.advancedcustomfields.com/forums/topic/using-acf-form-in-ajax-call/
Here’s one I contributed to that appears to ave been a help http://supp ort.advancedcustomfields.com/forums/topic/use-update_field-with-ajax/
hope some of this helps
Hi John,
Thanks, I was able to hook acf_form_head into:
wp_localize_script( 'csm-csf', 'acf_form_head', array( 'ajaxurl' => admin_url( 'admin-ajax.php', 'http' ) ) );
add_action( 'wp_ajax_nopriv_acf_form_head', 'acf_form_head' );
add_action( 'wp_ajax_acf_form_head', 'acf_form_head' );
And pass it data via ajax and get a response. Looks like building out the data for basic form elements is easy enough, but repeater fields and select2 off screen fields are going to take some work.
But I’ll look at other links you provided. Thanks!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.