PHP Code Error | Scroll to First Error Message on Gravity Form

Hey there!

We’re using the code snippet provided below (obtained here), but it contains a minor PHP code error (or two) that we can’t quite figure out how to solve.

The error messages we’re getting are:

PHP Syntax Check: Parse error: syntax error, unexpected ‘}’, expecting end of file in your code on line 4 (source)

and

Parse error: syntax error, unexpected ‘}’, expecting end of file in code on line 18 (source)

Any there any PHP experts out there that can tell us how to fix the errors?

Thank you!


Code Snippet:

function gf_scroll_to_first_error_focus( $form ) {
    ?>
    <script type="text/javascript">
        if( window['jQuery'] ) {
            ( function( $ ) {
                $( document ).bind( 'gform_post_render', function() {
                    var $firstError = $( 'li.gfield.gfield_error:first' );
                    if( $firstError.length > 0 ) {
                        $firstError.find( 'input, select, textarea' ).eq( 0 ).focus();
                        document.body.scrollTop = $firstError.offset().top;
                    }
                } );
            } )( jQuery );
        }
    </script>
    <?php
    return $form;
}
add_action( 'gform_pre_render', 'gf_scroll_to_first_error_focus', 10, 1 );

There doesn’t appear to be anything amiss with this code, unless there are hidden characters somehow. Do you encounter the same after ensuring the snippet opens with <?php at the beginning?

Hey @uamv,

Thanks for your help and lead.

Yes, we’re using Code Snippets (plugin) for the code and it hides <?php at the top.

Details: Screenshot by Lightshot

Using Code Snippets we get no errors. But when I add <?php programatically, it gives us an error.

Details: Screenshot by Lightshot

The funny thing is that when we run the code check using your suggestion and php checker, there’s no error given.

Details: Screenshot by Lightshot

I didn’t want to bring this up, but the above Code Snippet (now deactivated) was causing Google to flag one of our sitemap URLs, thus impacting our SEO. Note: Sorry, I did not have a chance to screenshot the Google error message.

So, not sure which way to go now unless you know of an alternate code snippet that can achieve what we need. That is … “Scroll to First Error Message” when clicking the SEND button.

Thoughts?

Again, thank you!

Hi Jonathan. I’m not sure why your code is throwing a syntax error exactly, but have you seen this solution that may accomplish the same thing for you?

Hey Chris,

Thank you for the reference. Yes, we ran into that article (code snippet) this morning.

We replaced ours with the one you referenced. Unfortunately, it partially worked for us.

We submitted our feedback to Gravity Wiz via the comment section of the article. Their response suggests they plan to make some adjustments or enhancements to the code.

As soon as Gravity Wiz can solve or address the issues we identified, we’ll leave the code snippet activated on our site(s).

Perhaps we can leave this topic open until the issues are addressed and/or fixed.

Again, thank you.

I’ll leave it open, but it will auto-close after 30 days. Send me a message and I’ll reopen it if it’s closed and you need to add to it. Thanks!

Thanks, Chris! Understood. Will advise soon.

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