Home › Forums › Backend Issues (wp-admin) › Use ACF field selection to update another ACF field › Reply To: Use ACF field selection to update another ACF field
I have this completely working now…
When a user is selected, the relevant user meta data is taken out of the database and used to populate 4 other fields.
Enqueue jquery and localise from functions.php
function enqueue_scripts_back_end(){
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my_query.js', array('jquery'));
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' )) );
}
add_action('admin_enqueue_scripts','enqueue_scripts_back_end');
seperate jQuery file…
jQuery(document).ready(function($) {
var field = acf.getField('field_5a7dc432d1999');
field.on('change', function( e ){
var vally = field.val();
var data = {
'action': 'my_action',
'userno': vally
};
$.ajax({
url: ajax_object.ajax_url,
type : 'post',
data: data,
dataType: 'json',
success: function( data ) {
$('#acf-field_5c3daeb8608ae').val(data.heightno);
$('#acf-field_5c3daf8f608af').val(data.weightno);
$('#acf-field_5c3dafc1608b1').val(data.ageno);
$('#acf-field_5c3dafa5608b0').val(data.bodyfatno);
}
})
});
});
in my functions.php file…
// Same handler function...
add_action( 'wp_ajax_my_action', 'my_action' );
function my_action() {
global $wpdb;
$userno = intval( $_POST['userno'] );
$heightno = get_user_meta( $userno, 'user_height', 'true' );
$weightno = get_user_meta( $userno, 'user_weight', 'true' );
$bodyfatno = get_user_meta( $userno, 'user_bodyfat', 'true' );
$dob = get_user_meta( $userno, 'user_dateofbirth', 'true' ); i
$dobno = explode( "/", $dob );
//get age from date or dob
$ageno = ( date( "md", date( "U", mktime( 0, 0, 0, $dobno[ 0 ], $dobno[ 1 ], $dobno[ 2 ] ) ) ) > date( "md" ) ?
( ( date( "Y" ) - $dobno[ 2 ] ) - 1 ) :
( date( "Y" ) - $dobno[ 2 ] ) );
echo json_encode(array('heightno' => $heightno,'weightno'=> $weightno, 'ageno'=> $ageno, 'bodyfatno'=> $bodyfatno));
wp_die();
}
Hope that helps somebody
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.