Gravity forms plugin - dynamically populating form field isn't working
Asked Answered
U

3

9

I'm having some problems with my Gravity Forms form. It's a multi page form, and I need to populate a field on page 2 using post values from page 1. But it's not working. Of course the field on page 2 is configured to "Allow field to be populated dynamically" and the field's parameter is set to "name". Here's my code:

add_filter('gform_field_value_name', 'name_population_function');
function name_population_function($value){
    $name = $_POST['input_2'] . ' ' . ( ! empty( $_POST['input_3'] ) ? ( $_POST['input_3'] . ' ' ) : NULL ) . $_POST['input_1'];
    return $name;
}

When I print the value of $name variable using var_dump($name); it's actually correct.

If I change the $name to $name = 'Last Middle First'; it's populating the field as it should.

Thank you for your help.

EDIT: For test purposes I changed my code to:

add_filter('gform_field_value_name', 'name_population_function');
function name_population_function($value){
    $name = 'Test';
    if ( ! empty( $_POST['input_1'] ) && ! empty( $_POST['input_2'] ) ) {
        $name = $_POST['input_2'] . ' ' . ( ! empty( $_POST['input_3'] ) ? ( $_POST['input_3'] . ' ' ) : NULL ) . $_POST['input_1'];
    //var_dump( $name );
    }
    return $name;
}

If I uncomment the line with var_dump the value of variable $name is again set correctly, but the field on page 2 is prepopulated with value Test. Probably this filter is also called on page 1 so is it possible that the plugin caches this value? I don't use a caching plugin, so don't know why it's not working.

Umlaut answered 17/11, 2012 at 18:29 Comment(3)
When you var dump, are you doing it on the second page, or on the first page?Ebullition
The var_dump was placed in name_population_function in function.php file right before the return and the first page was already sent.Umlaut
Sorry for posting late but in case someone else has this problem I found that using AJAX posting does not actually post content from page to page but just shows/hides parts of the form to give the appearance of pages. Disabling AJAX posting allowed me to populate fields from one page to a later page. If you really need AJAX posting enabled then you may need to use the JavaScript hooks to get this to work.Winfield
E
1

Rather than having the form over multiple pages, you could use jQuery to create the impression of multiple pages with .hide or .slideToggle. This would resolve your problem and make the submitting of the form data much easier. Then simply call the value from past inputs.

Emendation answered 28/6, 2014 at 13:36 Comment(0)
M
0

You could use a query string to persist the data and populate the field dynamically.

http://siteurl.com/form-url/?your_parameter=value

add_filter('gform_field_value_author_email', 'populate_post_author_email');
function populate_post_author_email($value){
global $post;

$author_email = get_the_author_meta('email', $post->post_author);

return $author_email;
}
Melar answered 17/11, 2013 at 5:21 Comment(0)
A
-1

Try this gform_pre_render filter instead. It adds a filter to form ID 7. Replace 7 with your Gravity form's ID.

add_filter('gform_pre_render_7', 'populate_form_pre_render');

function populate_form_pre_render($form){
  $name ='';
  foreach ($form['fields'] as &$field)
  {
     // replace 2 with the actual ID of your form field 
    if ( 2 == $field['id'] ){
      if ( ! empty( $_POST['input_1'] ) && ! empty( $_POST['input_2'] ) ) {
         $name = $_POST['input_2'] . ' ' . ( ! empty( $_POST['input_3'] ) ? ($_POST['input_3'] . ' ' ) : NULL ) . $_POST['input_1'];
        //var_dump( $name );
      }
      $field['defaultValue']=$name;
  echo '<pre>';
  print_r($field);
  echo '</pre>';
   }    

 }

 return $form;
}
Aubarta answered 14/10, 2013 at 21:34 Comment(2)
Why in foreach ($form['fields'] as &$field) are you passing $field by reference? I'm just wondering why do it?Precondition
Because you're changing the value of the field and then at the end of the function you're returning the modified $form object. If you passed by value your changes would be lost.Apure

© 2022 - 2024 — McMap. All rights reserved.