728x90

Viewed 197 times

 

-1

I want to use a multi-step form where the user enters their name in step one, then on step two the form will say "Hello, [name]" with the rest of the content and steps to follow.

Any ideas?

EDIT: I want to use data entered in step one on step 2. Reading GF documentation I think something like {[Field Name]:[field_id]} should work, but it does not. Digging further I found that this would only work once a form has been submitted already. So my question is, is there a way that will enable me to use the merge tags within the current form from data being submitted in a previous step?

pluginsplugin-gravity-forms

share  improve this question  follow 

edited Mar 30 '19 at 15:17

 

asked Mar 29 '19 at 20:17

 

Rowbee

32 bronze badges

  •  

    Welcome to WordPress development! Unfortunately it's unclear what you are asking as you didn't tell us what exactly you've tried so far and where you are stuck right now. Please update your question accordingly as otherwise it's just too broad. – leymannx Mar 30 '19 at 8:59

  •  

    That is fair. I am trying to use merge tags in a multi-step form. I want to use data entered in step one on step 2. Reading GF documentation I think something like {[Field Name]:[field_id]} should work, but it does not. Digging further I found that this would only work once a form has been submitted already. So my question is, is there a way that will enable me to use the merge tags within the current form from data being submitted in a previous step? – Rowbee Mar 30 '19 at 15:15 

 

 

0

This is possible with Live Merge Tags, a feature of Gravity Forms Populate Anything.

If you're looking to get your hands dirty with some code, Gravity Forms has an example of how this might work with a snippet on their docs for the gform_pre_render filter:

https://docs.gravityforms.com/gform_pre_render/#3-populate-field-with-values-from-earlier-page

 

gform_pre_render - Gravity Forms Documentation

The gform_pre_render filter is executed before the form is displayed and can be used to manipulate the Form Object prior to rendering the form.

docs.gravityforms.com

 

 

3. Populate Field With Values From Earlier Page

This example is for a two-page form. The data submitted from the first page is displayed on the second page as a preview. The second page has only one field, an html field that will be populated with the data from the first page.

add_filter( 'gform_pre_render_81', 'populate_html' );

function populate_html( $form ) {

    //this is a 2-page form with the data from page one being displayed in an html field on page 2

    $current_page = GFFormDisplay::get_current_page( $form['id'] );

    $html_content = "The information you have submitted is as follows:<br/><ul>";

    if ( $current_page == 2 ) {

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

            //gather form data to save into html field (id 6 on my form), exclude page break

            if ( $field->id != 6 && $field->type != 'page' ) {

                //see if this is a complex field (will have inputs)

                if ( is_array( $field->inputs ) ) {

                    //this is a complex fieldset (name, adress, etc.) - get individual field info

                    //get field's label and put individual input information in a comma-delimited list

                    $html_content .= '<li>' .$field->label . ' - ';

                    $num_in_array = count( $field->inputs );

                    $counter = 0;

                    foreach ( $field->inputs as $input ) {

                        $counter++;

                        //get name of individual field, replace period with underscore when pulling from post

                        $input_name = 'input_' . str_replace( '.', '_', $input['id'] );

                        $value = rgpost( $input_name );

                        $html_content .= $input['label'] . ': ' . $value;

                        if ( $counter < $num_in_array ) {

                            $html_content .= ', ';

                        }

                    }

                    $html_content .= "</li>";

                } else {

                    //this can be changed to be a switch statement if you need to handle each field type differently

                    //get the filename of file uploaded or post image uploaded

                    if ( $field->type == 'fileupload' || $field->type == 'post_image' ) {

                        $input_name = 'input_' . $field->id;

                        //before final submission, the image is stored in a temporary directory

                        //if displaying image in the html, point the img tag to the temporary location

                        $temp_filename = RGFormsModel::get_temp_filename( $form['id'], $input_name );

                        $uploaded_name = $temp_filename['uploaded_filename'];

                        $temp_location = RGFormsModel::get_upload_url( $form['id'] ) . '/tmp/' . $temp_filename['temp_filename'];

                        if ( !empty( $uploaded_name ) ) {

                            $html_content .= '<li>' . $field->label . ': ' . $uploaded_name . "<img src='" . $temp_location . "' height='200' width='200'></img></li>";

                        }

                    } else {

                        //get the label and then get the posted data for the field (this works for simple fields only - not the field groups like name and address)

                        $field_data = rgpost('input_' . $field->id );

                        if ( is_array( $field_data ) ){

                            //if data is an array, get individual input info

                            $html_content .= '<li>' . $field->label . ': ';

                            $num_in_array = count( $field_data );

                            $counter = 0;

                            foreach ( $field_data as $data ) {

                                $counter++;

                                $html_content .= print_r( $data, true );

                                if ( $counter < $num_in_array ) {

                                    $html_content .= ', ';

                                }

                            }

                            $html_content .= '</li>';

                        }

                        else {

                            $html_content .= '<li>' . $field->label . ': ' . $field_data . '</li>';

                        }

                    }

                }

            }

        }

        $html_content .= '</ul>';

        //loop back through form fields to get html field (id 6 on my form) that we are populating with the data gathered above

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

            //get html field

            if ( $field->id == 6 ) {

                //set the field content to the html

                $field->content = $html_content;

            }

        }

    }

    //return altered form so changes are displayed

    return $form;

}

 

+ Recent posts