Gravity form gform_confirmation script is running multiple times

Hello, I have a issue with a form that’s inside a popup on a particular page .
I have added a few lines of javascript through gform_confirmation_loaded function inside the gform_confirmation filter. But my javascript is running multiple times on confirmation.

This is my code.

add_filter( 'gform_confirmation_7', 'custom_popup_confirmation_for_sgtracking', 10, 4 );
function custom_popup_confirmation_for_sgtracking( $confirmation, $form, $entry, $ajax ) {



if ( ! is_string( $confirmation ) ) {
return $confirmation;
}
$popupstatus = rgar( $entry, '4' );
$emailvalue = rgar( $entry, '1' );
$test_bug = 'test_bug';
if($popupstatus == "true"){


$confirmation .= GFCommon::get_inline_script_tag( "window.top.jQuery(document).on('gform_confirmation_loaded', function () {console.log('$test_bug /running'); } );" );


}
return $confirmation;



}

Your JavaScript code appears to be executed multiple times due to the gform_confirmation_loaded event firing more than once. This can occur if there are multiple Gravity Forms on the page or the form is being submitted multiple times.

To prevent this from happening, you can add a flag to your code that will only allow the JavaScript to be executed once. Here is an updated version of your code that includes a flag:

add_filter( 'gform_confirmation_7', 'custom_popup_confirmation_for_sgtracking', 10, 4 );

function custom_popup_confirmation_for_sgtracking( $confirmation, $form, $entry, $ajax ) {
    // If confirmation is not a string, return it unchanged
    if ( ! is_string( $confirmation ) ) {
        return $confirmation;
    }

    // Get the popupstatus and emailvalue from the form entry
    $popupstatus = rgar( $entry, '4' );
    $emailvalue = rgar( $entry, '1' );
    $test_bug = 'test_bug';

    // If popupstatus is "true", add JavaScript code to the confirmation message
    if($popupstatus == "true"){
        $confirmation .= GFCommon::get_inline_script_tag( "
            // Check if the flag is already set to prevent multiple executions
            if (window.top.popupConfirmationLoaded) {
                console.log('$test_bug /skipping');
            } else {
                // Set the flag to true to prevent further executions
                window.top.popupConfirmationLoaded = true;
                console.log('$test_bug /running');
            }
        " );
    }

    // Return the updated confirmation message
    return $confirmation;
}

In this updated code, we have added a check for the window.top.popupConfirmationLoaded. If this flag is already set to true, then we know that the JavaScript code has already been executed and can be skipped. If the flag is not set, we set it to true and execute the JavaScript code.

This should ensure the JavaScript code is only executed once, preventing further issues.

2 Likes

Thank you so much for your quick reply and easy solution. This works well :partying_face:. Thanks again.

1 Like

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