Support

Account

Home Forums ACF PRO Ajax Callback Add Row To Repeater

Solved

Ajax Callback Add Row To Repeater

  • Good Morning,

    I hope someone can help point out probably the glaringly obvious!

    I have some code which I know works to add a row into a repeater field on a user (user meta):

    	$row = array(
    		'field_595ceca2bddf1'	=> $job_id,
    		'field_595cecbabddf2'	=> $job_title
    	);
    	$i = add_row('field_595cec87bddf0', $row, $user_id);

    This works great when used with the acf/save_post function.

    I therefore created an ajax script which is to do the same sort of thing.

    When I test the ajax script with a single text field added to a user, it works perfectly fine when using the following in the ajax callback function:
    update_user_meta( $user_id, 'test_field', $job_title ); # Update our user meta

    So I know the callback works, it’s grabbing the values etc.

    However, if I swap this over to:

    	# Insert data into the repeater
    	$row = array(
    		'field_595ceca2bddf1'	=> $job_id,
    		'field_595cecbabddf2'	=> $job_title
    	);
    	$i = add_row('field_595cec87bddf0', $row, $user_id);

    It simply doesn’t add the row to the user repeater field.

    Unless I’m missing something, I honestly can’t see why?

    Any help is much appreciated!

    Thanks in advanced.

  • Don’t know if it will help you but I have an example of dynamically adding rows to a repeater field using javascript and ajax here https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/dynamic-repeater-on-category

  • Thanks John Huebner

    I’ll take a look although it’s odd as the add_row code works inside the save_post function so not sure why it won’t in the ajax call

  • The ajax call may be creating the new row, but that will not add it to the admin unless you reload the page. ACF does not automatically refresh the content of the page after the ajax call.

  • But if it works for a single field why not a repeater (excuse my ignorance)

    Even if I check the DB, not info is added, not even a new empty row

  • Solved it!

    Replace:
    $i = add_row('field_595cec87bddf0', $row, $user_id);
    With:
    $i = add_row('field_595cec87bddf0', $row, 'user_'.$user_id);

    Note the user_ before the ID, otherwise it posts to the postmeta table not the usermeta table!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Ajax Callback Add Row To Repeater’ is closed to new replies.