skip to Main Content

I have a form built with gravity forms that asks 5 questions, each with 2 answers.

Each answer corresponds to a category, and so I want to be able to add some conditional logic so that the confirmation sends to a different page depending on which category has more answers.

Form is here: https://90daysfromretirement.com/medicare-advantage-vs-medicare-supplement/

The two categories are "Advantage" and "Supplement" – so dependng on the answer, so whichever category has more responses (3 or greater) then the confirmation will redirect them to the appropriate page.

Problem is I can’t figure out how to Count the values for each and run logic off it. I don’t think it’s possible with any gravity forms add-ons I’ve found, and I haven’t been able to put the code together to do it.

Any suggestions?

2

Answers


  1. Since you only have two options, you really only need to know the count of one and the total number of questions to know which value has been selected more.

    For example, if you have 10 questions and "Advantage" choices are selected 6 times, you know that Advantage has more than "Supplement".

    Here’s a snippet that should get you started. It will take a group of Radio Button fields, count the number of times a specified value has been selected, and populate that count into a Number field.

    You can then use that Number field to apply conditional logic on your confirmations to redirect to the appropriate page (e.g. If Count is greater than 5, redirect to Advantage page. If Count is less than 6, redirect to Supplement page).

    /**
     * Gravity Wiz // Gravity Forms // Value Counter
     * https://gravitywiz.com/
     * 
     * Count the number of times a given value has been selected in a group of fields and populate that number into a Number field.
     * This snippet is designed to target a Number field and count selected values in Checkbox and Radio Button fields.
     * 
     * This snippet works best with our free [GF Custom Javascript](https://gravitywiz.com/gravity-forms-custom-javascript/) plugin.
     */
    // Replace the "1", "2" and "3" with field IDs of fields that should have their selected values counted. If you are using the 
    var $radios    = jQuery( '#field_GFFORMID_1, #field_GFFORMID_2, #field_GFFORMID_3' );
    // Replace "4" with the ID of the Number field in which the count should be populated.
    var $target    = jQuery( '#field_GFFORMID_4' );
    // Replace "a" with the value you wish to count if selected.
    var countValue = 'a';
    
    function gwRecountValues() {
        $target.find( 'input' ).val( $radios.find( 'input:checked[value="' + countValue + '"]' ).length );
    }
    
    $radios.on( 'change', function() {
        gwRecountValues();
    } );
    
    gwRecountValues();
    

    And here’s the source which may be updated in the future if other folks use this and experience any issues or need improvements: https://github.com/gravitywiz/snippet-library/commit/d34eb169da937981c4a1ea49bb8a36df023bff1b

    The one thing missing from this snippet is PHP validation. You may not be concerned about people altering their count but it is possible without PHP validation.

    Login or Signup to reply.
  2. You could try adding the following to your functions.php file

    add_action( "gform_after_submission_66", "after_submission", 10, 2 ); //replace 66 with your form number
        function after_submission($entry, $form ){
            $supplement_answers = array("milk", "apple", "pasta", "fries", "oats"); //replace array items with the supplement answers
            $advantage_answers = array("red", "blue", "pink", "purple", "yellow"); //replace array items with the advantage answers
            $field_nums = array("2","3","6","7","9"); //replace these field numbers with your own -- order then to align with answers above
            $count_fields = count($field_nums);
            $a = 0;
            $b = 0;
            for($i=0; $i<$count_fields; $i++){
                if($entry[$field_nums[$i]] == $supplement_answers[$i]){
                    $a = $a + 1;
                }else if($entry[$field_nums[$i]] == $advantage_answers[$i]){
                    $b = $b + 1;
                }
            }
            if($a > $b){
                 header('Location: https://website/supplements_page'); //replace url
            }else if($a < $b){
                 header('https://website/advantage_page'); //replace url -
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search