6

I have Ninja Forms and ACF for WordPress installed. I have a hidden field in Ninja Forms and I need to repopulate this with the value from an ACF field.

I tried simple jQuery but it doesn't work:

$('input[name=nf-field-19]').val('<?php the_field('rsvp_email'); ?>');

As you can tell.. I'm no PHP or jQuery guy.. trying to fiddle around and find a solution.

Thanks!

conradical
  • 95
  • 1
  • 4

2 Answers2

10

Do you need this to work from JS for some reason? I am using the ninja_forms_render_default_value filter hook to prepopulate hidden form fields:

/**
 * Populate hidden input with ACF values
 */
function nf_hidden_field_values( $value, $field_type, $field_settings ) {
    global $post;
    $value = '';
    if ( $field_settings['key'] == 'hidden_field_1' ) {
        $value = get_field('acf_field_1', $post->ID);
    }

    if ( $field_settings['key'] == 'hidden_field_2' ) {
        $value = get_field('acf_field_2', $post->ID);
    }

    return $value;
}
add_filter( 'ninja_forms_render_default_value', 'nf_hidden_field_values', 10, 3 );
cr0ybot
  • 3,960
  • 2
  • 20
  • 22
0

You can do it with Jquery you just need to trigger a change event :

var fieldID = 197;
var newValue = 'compactpro';
jQuery( '#nf-field-' + fieldID ).val( newValue ).trigger( 'change' );

Ressource : https://developer.ninjaforms.com/codex/changing-field-values/

Cédric
  • 9
  • 1
  • 1
    Actually it seems to not working anymore since the Ninja Forms 3 Update, you should use the solution of crObot. – Cédric Dec 06 '18 at 11:02