Home › Forums › Add-ons › Flexible Content Field › Flexible Fields + AJAX save fields on true/false action
I’m using Flexible Content and I want to save the current row’s field when a ACF true/false field in the current row is checked/true.
I have the AJAX call and the return function working but I just can’t figure out how to update the current row fields, by this point the post is not save so I can’t use whilst have rows because they don’t exsisit yet
JQUERY
jQuery(document).ready(function($){
if (typeof acf == 'undefined') { return; }
var autoSaveACFextension = acf.ajax.extend({
events: {
'change [data-key="field_5ac62826fe37e"] .acf-true-false input': 'state_change',
},
state_change: function(e){
var checked;
var true_false = jQuery('[data-key="field_5ac62826fe37e"] input[type="checkbox"]');
var parentName = jQuery('[data-key="field_5ac761f95f7d0"] .acf-input-wrap input[type="text"]');
var parentValue;
parentName.each(function( index ) {
if($(this).val().length > 0){
parentValue = $(this).val();
}
});
if( this.state_request) {
this.state_request.abort();
}
if (jQuery(true_false).is(":checked")){
checked = true;
} else {
checked = false;
}
var self = this,
data = this.o;
console.log(parentValue);
data.action = 'autosave';
data.active = checked;
data.parent = parentValue;
data.exists = [];
this.state_request = jQuery.ajax({
url: acf.get('ajaxurl'),
data: acf.prepare_for_ajax(data),
type: 'post',
dataType: 'json',
async: true,
success: function(json){
console.log("success", json);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus);
console.log("Error: " + errorThrown);
}
});
},
});
});
FUNCTION
<?php
new autosave_acf_extension();
class autosave_acf_extension {
public function __construct() {
add_action('wp_ajax_nopriv_autosave',array($this, 'autosave'));
add_action('wp_ajax_autosave', array($this, 'autosave' ));
add_action('acf/input/admin_enqueue_scripts', array($this, 'autosave_enqueue_script'));
}
public function autosave_enqueue_script() {
global $post;
if (!$post || !isset($post->ID) || get_post_type($post->ID) != 'post') {
return;
}
$handle = 'autosave-acf-extension';
$src = get_template_directory_uri().'/assets/js/admin/work-acf-autosave.js';
$depends = array('acf-input');
wp_enqueue_script($handle, $src, $depends);
}
public function autosave() {
if (!wp_verify_nonce($_POST['nonce'], 'acf_nonce')) {
die();
}
$post_id = $_POST['post_id'];
// error_log( print_r($_POST,true), 3, "/Applications/MAMP/logs/php_error.log");
if( have_rows('field_5975fd76ba7e3', $post_id) ) {
$i = 0;
while( have_rows('field_5975fd76ba7e3', $post_id) ) {
if( get_row_layout() == 'ad' ):
endif;
the_row();
$i++;
update_sub_field('ad_name', "This caption is in row {$i}", $post_id);
}
}
echo json_encode($_POST['post_id'], $_POST['parent']);
exit;
// error_log( print_r("e",true), 3, "/Applications/MAMP/logs/php_error.log");
}
}
?>
The topic ‘Flexible Fields + AJAX save fields on true/false action’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.