PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

How to prepopulate a field based on a previous field in the same form

  1. I've been looking around in the forums for a while and am using the code snippet from this post:

    http://www.gravityhelp.com/forums/topic/populate-field-from-previous-field

    This is my version of the code from functions.php, but it's not working:

    // update "108" to the ID of your form
    add_filter("gform_pre_render_2", "populate_previous_page_data");
    function populate_previous_page_data($form){
    
        $page_number = rgpost("gform_target_page_number_{$form["id"]}");
        $page_number = !is_numeric($page_number) ? 1 : $page_number;
    
        foreach($form['fields'] as &$field) {
    
            if($field['id'] != 42.5) // update "15" to the ID of the field you wish to be populated;
    
            $field_page = rgar($field, 'pageNumber');
    
            if($page_number > $field_page)
                continue;
    
            // update this to the ID of the field whose value you wish to call
            // '16.3' = example, first name of a Name field
            // '16.6' = example, last name of a Name field
            $field['defaultValue'] = rgpost('1');
        }
        return $form;
    }

    My form is here, and I'm trying to prepopulate the Zip code field on the last page with the value entered on the first page. Can someone help?

    Posted 12 years ago on Friday July 20, 2012 | Permalink
  2. tylerrum
    Member

    I'm trying to accomplish a similar goal but for a form on the same page as opposed to different pages. I have multiple sections on one form that are all identical but only show up (i.e., are conditional fields) when a user clicks "Add another property". I'd like these new property fields to autopopulate with the info put in for the previous property. I've read around a bit, but I'm definitely a rookie when it comes to editing PHP or figuring out where to post the code snippets I've seen and how to properly format them. Any help would be great.

    Posted 12 years ago on Friday July 20, 2012 | Permalink
  3. For copying the value between fields on the same page, please see this:
    http://www.gravityhelp.com/forums/topic/billing-same-as-shipping-address

    Posted 12 years ago on Saturday July 21, 2012 | Permalink

This topic has been resolved and has been closed to new replies.