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.

Conditional Logic and Pre Population

  1. Hey Guys -

    My website sells multiple tours - some of these tours depart from multiple departure locations and all have various little unique differences that are reflected in the forms.

    So the forms aid the user experience, so that the interaction with Salesforce is smooth, and also so that it is easier for me to make large scale changes to forms I have decided to make a 'super' form that can be used on all tour pages (and then a couple others for non tour pages). For this to work there will be various entry fields that are conditional - many of these fields would be conditional on a field containing the name of the tour the user is interested in.

    The problem is my Boss doesn't want the user to have to choose the tour they are interested in, so I was wondering if there was some way I can pre-populate a field with the tour/page name (or perhaps default to the relevant tour?) and yet still have other fields be able to rely on it for conditional logic.

    An example of two different forms:

    http://www.fraserfree.com.au/fraser-island-tours-hervey-bay/fraser-island-tour-2-day-experience
    http://www.fraserfree.com.au/fraser-island-tours-hervey-bay/fraser-island-tour-3-day-cool-dingo

    Any idea greatly appreciated!

    Posted 13 years ago on Wednesday October 12, 2011 | Permalink
  2. I have decided to have a form on a separate page - so I can send any necessary pre-population via a query string when a user clicks the enquire button.

    Seems like a simpler way of doing this!

    Posted 13 years ago on Thursday October 13, 2011 | Permalink
  3. Other choice would be to use the shortcode to populate these fields.

    http://www.gravityhelp.com/documentation/page/Using_Dynamic_Population

    Pass the tour choice to the form when you embed it into the page and you can use the same form for every tour - [gravityform id=1 field_values='tour_choice=2day']

    Posted 13 years ago on Thursday October 13, 2011 | Permalink
  4. Hey Scott. Thanks for your reply.

    Is it possible to use the shortcode when I use the Gravity Forms Widget Plugin for the forms?

    I attempted to pre-populate using a hook in my functions.php file but I failed miserably! I am not sure what to put in the brackets after 'get_page'. What I came up with is:

    [php]
    add_filter('gform_field_value_which_tour', 'populate_tour_name');
        global $post;
        $which_tour = get_page();
        return $post_title;
    }

    Thanks again!

    Posted 13 years ago on Thursday October 13, 2011 | Permalink
  5. With a widget the shortcode call variables won't work - only choices are hooks like you are working with, referral arguments, or (probably easiest for your form) the pre-configured default values.

    If you name the Post / Page with the tour's identity, you can use the default value - insert variable field on an admin only (to hide from visitors) field. - http://grab.by/b3FS

    This does limit you to a single line or similar field, if you want to do a drop down or radio button field you will have to go with the hook.

    As for your hook, it can be done linking to the internal WordPress function get_the_title()

    /*---------------------------------------------------------------------------------*/
    /* Gravity Forms Functions - Get page fields from WordPress                        */
    /*---------------------------------------------------------------------------------*/
    
    //  Assigns the name of the current post/page to Gravity Forms field 'which_tour'
    add_filter('gform_field_value_which_tour', 'get_the_title');

    One limit I have notices with this is if the widget is displayed on a category list it will capture the name of the oldest post in the category.

    Posted 13 years ago on Thursday October 13, 2011 | Permalink
  6. Hey Scott.

    I originally wanted to just use the Insert Variable function but I need to have other fields be conditional on the tours name. I don't seem to be able to choose a conditional against a variable field.

    I am also having trouble with the hook. I placed what you said in my functions.php file but it doesn't seem to do anything - since it is an internal wordpress function does that mean I don't need the function and return code afterwards? Sorry I am only a beginner at php.

    Posted 13 years ago on Friday October 14, 2011 | Permalink
  7. No problem, I'm new to php too but I've always been good at tearing apart coding.

    Most of the snipets you find here are creating a custom function, and then passing the result to the Gravity Forms variables. To do this there are two parts - the function, and a statement to assign the result to a variable.

    In this case, the WordPress function get_the_title() already exists, so you do not have to create the function and assign the variable to return.

    The add_filter() function needs to be encased in php coding in the functions.php file. You can ether copy the single line into the existing functions list, or add it to the end of the file with it's own php call.

    <?php
    add_filter('gform_field_value_which_tour', 'get_the_title');
    ?>

    Check the 'Allow field to be populated dynamically' and then enter which_tour in the box.

    Be careful editing the functions.php - if the code has an error in it it may stop the WordPress engine from working and you will need to correct the error from outside the WordPress editor (control panel, ftp, etc).

    Posted 13 years ago on Sunday October 16, 2011 | Permalink
  8. Scott - thank you so much!

    It was simply the placement of the line in the php file that I believe was causing it to not work.

    Your help is much appreciated.

    Posted 13 years ago on Tuesday October 18, 2011 | Permalink
  9. Thank you Scott, and congrats on getting it working mgeldard.

    Posted 13 years ago on Tuesday October 18, 2011 | Permalink

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