Home › Forums › Add-ons › Repeater Field › Load Repeater Images via AJAX › Reply To: Load Repeater Images via AJAX
As if by magic, I’ve managed to pull the values in from my repeater field! This is the code I’m using:
add_action('wp_ajax_data_fetch', 'data_fetch'); // logged in users
add_action('wp_ajax_nopriv_data_fetch', 'data_fetch'); // other users
function data_fetch(){
$post_id = filter_input( INPUT_POST, 'post_id' ); // post id passed from select value via ajax
$array = array(); // setup empty array
$rows = get_field('new_colour', $post_id); // repeater field
if($rows)
{
foreach($rows as $row) // foreach for each repeater row
{
$array[] = '<li data-id="' . esc_attr( $row['unique_id'] ) . '"><label><input type="checkbox" value="' . get_the_title($post_id) . " - " . $row['colour_name'] . '">' . $row['colour_name'] . '</label></li>'; // stuff I want to display on the div I'm appending the repeater values to
}
}
wp_send_json($array); //Use WP send json function - send data back to the page I'm displaying the div on
}
$field.change(function () { // $field = select dropdown
var post_id = $(this).val(); // value of select dropdown = page id where repeater is located
var data = {
action : 'data_fetch',
'post_id' : post_id
}
var $customList = $(this).closest('.acf-row').find('#modal .checkbox-list-custom'); // element I'm displaying data in (i have multiple divs I want to populate with values)
$.post( ajaxurl, data ).done(function(result) {
$customList.empty(); // emptying the div of any previous data
$customList.append(result); // appending results to div
});
$modal.removeClass('visible'); // hide other open divs for additional select relationship field drop downs
$(this).closest('.acf-row').find('#modal:eq(0)').addClass('visible'); // making relevant div visible after the above has completed
});
Ended up swapping the while loop for a foreach loop, which for some reason made all of my code start working. Not really sure why, but my headache has disappeared!
The only thing I’ve not yet done is add in the relevant code to save the metadata to the post, but I’ll try and get that working correctly tomorrow probably.
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.