Home › Forums › General Issues › update_field() with Ajax › Reply To: update_field() with Ajax
In your second code snippet, you will need to provide the post ID of the post you’re updating.
function test_function() {
$input_test = $_POST['input-test'];
if (!isset($input_test) || $input_test == "") { $input_test = "Fall Back"; }
update_field('field-name', $input_test, $post_id); // specify post ID
wp_die();
}
add_action( 'wp_ajax_nopriv_test_function', 'test_function' );
add_action( 'wp_ajax_test_function','test_function' );
You will either need to add a hidden field your form that has the post ID and submits it or you will need to localize your script with the current post ID that you can add to the form submission. I’d probably go with the hidden post ID field because of the way you script submits the form.
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.