Home › Forums › General Issues › Saving data to ACFs using AJAX POST
Hello,
I created a front-end form that allows a user to enter data to create a new flight log entry. I’m using AJAX to create the post. While the post is created and shows the title and content, I can’t get the data to save to the Advanced Custom Fields. I’ve registered the field groups and I’m using the field keys with update_field(). I’m not certain what I’m overlooking. Any help would be greatly appreciated.
createLogEntry(e){
var departure_field = $(“.new-flight-departure”).val();
var arrival_field = $(“.new-flight-arrival”).val();
var title = (date + ‘ ‘ + departure_field + ‘ ‘ + arrival_field);
var content = $(“.new-flight-text”).val();
$.ajax({
beforeSend: (xhr) => {
xhr.setRequestHeader(‘X-WP-NONCE’, flightData.nonce);
},
url: flightData.root_url + ‘/wp-json/wp/v2/flight/’,
type: ‘POST’,
data: {
action: ‘submit_log_entry’,
title: title,
departure_field: departure_field, //Advanced Custom Field
arrival_field: arrival_field, //Advanced Custom Field
content: content
},
success: (response) => {
console.log(“Congrats”);
console.log(response);
alert(‘Post has been saved.’);
},
error: (response) => {
console.log(“Sorry”);
console.log(response);
}
});
}
function submit_log_entry() {
$title = $_POST[‘title’];
$content = $_POST[‘content’];
$departure = $_POST[‘departure_field’];
$arrival = $_POST[‘arrival-field’];
$post_data = array(
‘post_title’ => $title,
‘post_content’ => $content,
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’
);
$post_id = wp_insert_post($post_data);
//Update departure field
$field_key = ‘field_5ecb28b30812a’;
update_field($field_key, $departure ,$post_id);
//Update arrival field
$field_key = ‘field_5ecc33eb92cf4’;
update_field($field_key, $arrival ,$post_id);
die();
}
add_action( ‘wp_ajax_nopriv_submit_log_entry’, ‘submit_log_entry’ );
add_action( ‘wp_ajax_submit_log_entry’,’submit_log_entry’ );
On first glance, it looks like $arrival = $_POST['arrival-field'];
is different than what’s being sent in the data object (arrival_field: arrival_field
). One has a hyphen, while the other has an underscore.
Can you verify that the ACF data is being successfully sent along to the function? By printing or console logging?
Thank you for taking the time to look at this. Here’s a snippet from the console log. It looks like the data for the ACF fields is empty.
_links: Object { self: (1) […], collection: (1) […], about: (1) […], … }
aircraft: “”
arrival_field: “”
content: Object { raw: “Test remarks.”, rendered: “<p>Test remarks.</p>\n”, protected: false, … }
date: “”
date_gmt: “2020-09-30T01:29:32”
departure_field: “”
thank you for accepting me in your prestigious forum . it is a delight for me to be there with you and i shall be able to find myself with diverse news about the topic that are raising in this forum .
i wanted to describe myself, my friend is mike , i have been publishing all the items debated in the forum for a great life . i had not had the opportunities to creating an identity , but due to the interesting history you display online , i decide to do do and post an invited to be received by you .
for me it has been a real enriching experience to be in this forum , it has provided me the chance to have small discussions with peers who are additionally interested in this kind of topic . it has given opens the gates for me to hear different ideas and opinion .
i will further be happy to supply you with all the necessary material in the forum , such that you could have a report and there is a net , and that both you and i can use all the topics that may be share .
i have been very attentive to this forum , that is so , yet again i give you what grateful i am for had accepted my request to the forum . i shall leave you my profile address and some pages there i met interesting news .
i am a person who pays to researcher and reading about this topic , besides it is important for me to share this knowledge with members and families . i miss the welcome that they giving to new member to the forum .
i hope more boards like this will be creation , so that everyone has accessibility to this important information . i have therefore created the area available to other people such that they may create an account and be accepted in this forum .
i say goodbye , be placing the need to shares any data about this forum , i wil be constantly active to be continually update . twice , it is a pleasure to be part of this forum , see you later !
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.