Apeal Tiwari
Apeal Tiwari

Reputation: 154

How do i poulate a field with a parameter from previous page in a multipage form in gravityforms?

I want to build a multipage from.

The first page asks for first name and last name.

I want to greet the user with his first name in the second page.

Upvotes: 0

Views: 1501

Answers (2)

fxrgc
fxrgc

Reputation: 110

If avoiding another plugin (as useful as that one is), you can use either the pre_submission_filter or pre_submission hooks to do this.

If their name was field 1 and lets say the field you'd like to show is field 2...

// THESE FOUR FILTERS WORK TOGETHER TO PRE-POPULATE ALL SORTS OF STUFF, AND YOU CAN ADD TO THIS AS NECESSARY.  MINE IS ABOUT 1500 LINES LONG AND IS USED BY SEVERAL FORMS.
add_filter('gform_pre_render', 'populate_forms');
add_filter('gform_pre_validation', 'populate_forms');
add_filter('gform_pre_submission_filter', 'populate_forms', 10);
add_filter('gform_admin_pre_render', 'populate_forms');
function populate_forms($form) {

    $form_id = $form['id'];
    $current_form = 2;  // pretending the form id you are working on is 2.
    $future_form = 10; // imaginary form you'll create later for another purpose.

    switch($form_id) {

        case $current_form:
            $first_name = !empty(rgpost('input_1_3')) ? rgpost('input_1_3') : null;  // gets the value they entered into the first-name box of field 1.

            foreach ($form['fields'] as &$field) {

                if ($field->id === '2') {  // Make as many of these as necessary.
                    if ($first_name) { // make sure there's actually a value provided from field 1.

                        $field->placeholder = $first_name;  // not necessary, just habit since sometimes you'd need to have a placeholder to reliably populate some fields.  
                        $field->defaultValue = $first_name;  // this is the piece that will actually fill in the value like you'd expect to see in your question.
                    }
                }
            }
            break;

        //case $future_form: do more stuff.
          //break;
    }
    return $form;
}

That should be a decent start for your functionality plugin where you can populate the current and future forms without much hassle. This can also be done with the gform_field_value hook; I've always found the language a bit clumsy with that one, personally.

The plugin mentioned earlier is definitely neat, but I found myself wanting to rely on that stuff less and less.

Upvotes: 0

Dave from Gravity Wiz
Dave from Gravity Wiz

Reputation: 2869

The best way to do this is to use Live Merge Tags with Populate Anything:

https://gravitywiz.com/documentation/gravity-forms-populate-anything/#live-merge-tags

If you collected the user's first name in a Name field on page 1, you could great him in the field label for a field on page 2 like so:

Hello, @{Name (First):1.3}

(In this example, the field ID for the Name field is 1. The 3 refers to the first name input of a Name field and will always be 3).

Upvotes: 1

Related Questions