PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Scroll to Form After Submit

  1. Patrick DeVivo
    Member

    Hi there,
    I have a small problem which has been somewhat of a nuisance for me the past hour or so. I have a form up at http://patrickdevivo.com/ which is kind of low down on the page. When a user submits it with no errors, everything works fine (try it out, I don't mind). However, if a user makes a mistake, then the form fields are highlighted with errors, but the user is stuck up at the top of the page and probably won't immediately think to scroll down to the form. Do you see what I'm saying?

    I'm wondering if there is some way to recognize either when a submission has errors, or just when a submission has been made in general, and from that execute a little php which executes a little jquery to scroll down to the form. Do you see?

    I'm by no means a PHP expert, but any advice would be very helpful.

    Thanks,
    Patrick

    Posted 14 years ago on Saturday April 17, 2010 | Permalink
  2. Patrick DeVivo
    Member

    I was able to fix it on my own. Just used some jQuery:

    if ($('.validation_error').length > 0) {<br />
          $(window).scrollTo( '#contact', {easing:'easeOutBounce'} );<br />
    }
    Posted 14 years ago on Saturday April 17, 2010 | Permalink
  3. Nice job. Thanks for sharing your script.

    Posted 14 years ago on Saturday April 17, 2010 | Permalink
  4. awarner20
    Member

    Patrick or Kevin,

    Can either of you verify where this code snippet should go?

    @Patrick, thanks for sharing your solution!

    Posted 14 years ago on Monday April 19, 2010 | Permalink
  5. Patrick DeVivo
    Member

    @awarner, you need to have jquery and the ScrollTo plugin loaded on your page. You should use wp_enqueue_script() to load it into the header of your page (or footer). But first, drop this code into your theme's functions.php (loads jquery from Google's CDN:

    <?php
    function my_init_method() {
    
    wp_deregister_script( 'jquery' );
    wp_register_script(   'jquery'
        , 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');
    }    
    
    add_action('init', 'my_init_method');
    ?>

    then add this into your header.php, maybe right before </header>:

    <?php wp_enqueue_script( 'jquery'); ?>

    then load in jquery ScrollTo, put the following right above </header> <script type="text/javascript" scr="http://flesler-plugins.googlecode.com/files/jquery.scrollTo-1.4.2-min.js"></script>

    (you should probably download it and serve it from your own server)

    And then after that add the following right below, still above </header>

    <script type="text/javascript">
    if ($('.validation_error').length > 0) {
    $(window).scrollTo( '#contact');
    }
    </script>

    where #contact is the id of the element your form is in. Or, if your form ID in gravityforms is 3 say, you could just use #gform_3

    I don't know if that helps. Hopefully it works. I'm by no means a jquery or javascript expert either.

    Posted 14 years ago on Monday April 19, 2010 | Permalink
  6. awarner20
    Member

    Thanks so much for the detailed instructions Patrick, most appreciated! I will be going live with a new site in a month or so and I'm planning on creating quite a few forms. This will be very helpful from a user's perspective.

    Thanks again, I'll let you know when I get to this:)

    Posted 14 years ago on Tuesday April 20, 2010 | Permalink
  7. Patrick DeVivo
    Member

    Glad I could help. Let me know if you need further explanation, or if I'm insulting your intelligence by telling you too much stuff you already know :)

    Posted 14 years ago on Tuesday April 20, 2010 | Permalink