Conditional Confirmation Based on Survey Score

I’m trying to set up a conditional confirmation that directs people to a different page depending upon their Likert Survey Score. We have scoring activated. I added this PHP to my functions.php but it always redirects people to /blog/ regardless of their score. Any ideas if what I’m trying to do is possible?

add_filter( ‘gform_confirmation_80’, ‘custom_confirmation_logic’, 10, 4 );
function custom_confirmation_logic( $confirmation, $form, $entry, $ajax ) {
// Accessing the total survey score from the entry
$total_score = rgar( $entry, ‘survey_total_score’ );

// Conditional redirection based on the score
if ( $total_score <= 8 ) {
    $confirmation = array( 'redirect' => site_url( '/blog/' ) );
} elseif ( $total_score >= 9 ) {
    $confirmation = array( 'redirect' => site_url( '/contact/' ) );
}

return $confirmation;

}

Hi, Pat.

Considering that the conditional evaluates the score and entirely depends on its value, something in the middle may be ‘not coming right’.

Try debugging the $total_score returned for each submission and see what value is being returned each time, the data type, etc.

That may help shed some light on what is happening during the evaluation.

Hey Pat, :wave:

Make sure you’re grabbing the right survey score first. If the score is 9 or more, send them to the contact page. Otherwise, send them to the blog.

add_filter('gform_confirmation_80', 'custom_confirmation_logic', 10, 4);

function custom_confirmation_logic($confirmation, $form, $entry, $ajax) {
    $total_score = rgar($entry, 'survey_total_score');

    if ($total_score >= 9) {
        $confirmation = array('redirect' => site_url('/contact/'));
    } else {
        $confirmation = array('redirect' => site_url('/blog/'));
    }

    return $confirmation;
}

You can also add a var_dump($total_score); in your function to debug and see the actual result of $total_score.

Best Regards
Faisal

Thanks Faisal and Juan for your suggestions. Faisal I used var_dump and got this result: string(0) “”. I also tried using this PHP code below using the field ID but it also returned a blank value.

On the Confirmation settings if I enter {survey_total_score} or {score:id=1} for the Pass Field Data via Query String it properly appends the URL with the total score. I reached out to Gravity Form support for help.

    // Accessing the score from the field with ID 1
    $score_field_id = '1'; // Adjust this to the correct field ID
    $score = rgar( $entry, $score_field_id );
1 Like

I figured it out! Gravity forms support supplied the variable for the survey total score: gsurvey_score.

So this is my revised code.

add_filter('gform_confirmation_80', 'custom_confirmation_logic', 10, 4);

function custom_confirmation_logic($confirmation, $form, $entry, $ajax) {
    $total_score = rgar($entry, 'gsurvey_score');

    if ($total_score >= 9) {
        $confirmation = array('redirect' => site_url('/high-score/'));
    } else {
        $confirmation = array('redirect' => site_url('/low-score/'));
    }

    return $confirmation;
}
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.