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

**URL:** <https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670>\
**Category:** Get Help\
**Tags:** php, validation-error, gravity-wiz\
**Created:** [October 19, 2022, 6:11pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670 "2022-10-19T18:11:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AeroStar](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/aerostar/32/15243_2.png) [@AeroStar](https://community.gravityforms.com/u/AeroStar)\
**Post date:** [October 19, 2022, 6:11pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/1 "2022-10-19T18:11:31Z")

</div>

Hey there!

We’re using the code snippet provided below ([obtained here](https://zeropointdevelopment.com/how-to-scroll-to-the-first-error-on-a-gravity-form/)), 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](https://prnt.sc/8Bw_BE6Jfj-o))

and

> Parse error: syntax error, unexpected ‘}’, expecting end of file in code on line 18 ([source](https://prnt.sc/s5HIQR4NMhJb))

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

Thank you!

* * *

**Code Snippet:**

```auto
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 );

```

---

<div class="post-metadata">

**Author:** ![uamv](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/uamv/32/1412_2.png) [@uamv](https://community.gravityforms.com/u/uamv)\
**Post date:** [October 19, 2022, 7:29pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/2 "2022-10-19T19:29:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AeroStar](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/aerostar/32/15243_2.png) [@AeroStar](https://community.gravityforms.com/u/AeroStar)\
**Post date:** [October 19, 2022, 8:19pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/3 "2022-10-19T20:19:19Z")

</div>

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](https://prnt.sc/ZUSOvYnNVqzE)

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

**Details:** [Screenshot by Lightshot](https://prnt.sc/PLRpIx9GUtlz)

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](https://prnt.sc/SMFg8Cqez8LI)

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!

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [October 20, 2022, 7:03pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/4 "2022-10-20T19:03:15Z")

</div>

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?

> **[Make Your Gravity Forms Validation Errors Mobile-friendly(er) - Gravity Wiz](https://gravitywiz.com/make-gravity-forms-validation-errors-mobile-friendlyer/)**
>
> Improve Gravity Forms validation errors for mobile users by automatically focusing on the first field with a validation error.

---

<div class="post-metadata">

**Author:** ![AeroStar](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/aerostar/32/15243_2.png) [@AeroStar](https://community.gravityforms.com/u/AeroStar)\
**Post date:** [October 20, 2022, 7:47pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/5 "2022-10-20T19:47:58Z")

</div>

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](https://gravitywiz.com/make-gravity-forms-validation-errors-mobile-friendlyer/#comment-843746) to Gravity Wiz via the comment section of the article. [Their response](https://gravitywiz.com/make-gravity-forms-validation-errors-mobile-friendlyer/#comment-843762) 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.

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [October 20, 2022, 7:48pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/6 "2022-10-20T19:48:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![AeroStar](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/aerostar/32/15243_2.png) [@AeroStar](https://community.gravityforms.com/u/AeroStar)\
**Post date:** [October 21, 2022, 2:26pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/7 "2022-10-21T14:26:22Z")

</div>

Thanks, Chris! Understood. Will advise soon.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [November 20, 2022, 2:26pm UTC](https://community.gravityforms.com/t/php-code-error-scroll-to-first-error-message-on-gravity-form/13670/8 "2022-11-20T14:26:48Z")

</div>

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