Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How to stop form submit from refreshing to the top of the page (modular)?

Started by J M 9 years ago · 8 replies · 21874 views
9 years ago

Sorry if the title is confusing. Basically I have a modular form and when you click submit the page refreshes (which is fine) but it sends the user to the top of the page - and since my form is at the bottom, this is a pain. Is there a way I can make the page automatically refresh to the form instead?

I've tried using the ScrolltoTop js plugin in my base.html.twig file but it doesn't seem to recognise the class name I used for the submit button in the form's modular.md file. If you're curious I'll post the code below:

This is in the modular.md file:

TXT
buttons:
  • type: submit
    value: Submit
    classes: form-button

This is in the base.html.twig file:

$('.form-button').click( function() {
$('html, body').animate({
scrollTop: $('#form').offset().top
}, 400);
});

I imagine there is a simple solution to this but I couldn't find anything. Thank you!

9 years ago

Hi
If you use ajax submit you won't have that annoying page refresh - its easy to change over - the instructions are here: how-to-ajax-submission

Then you probably don't really need to scroll anywhere but for me the submission message comes at the end of the page so I scroll down to the submission message so the user don't miss it. Basically its the same code from the learn site with an added scrollto at the end.

JS
<script>
$(document).ready(function(){

    var form = $('#contact-us');
    form.submit(function(e) {
        // prevent form submission
        e.preventDefault();

        // submit the form via Ajax
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            dataType: 'html',
            data: form.serialize(),
            success: function(result) {
                // Inject the result in the HTML
                $('#form-result').html(result);
            },
            complete: function(result) {
                $('html, body').animate({
                    scrollTop: $("#form-result").offset().top
                }, 2000);   
            }
        });
    });

});
</script>
👍 1
9 years ago

I'm experiencing the same problem as described in the main post. I've read the documentation, but the instructions are not very clear for me. Where should I paste the following code:

JS
<div id="form-result"></div>

<script>
$(document).ready(function(){

    var form = $('#ajax-test-form');
    form.submit(function(e) {
        // prevent form submission
        e.preventDefault();

        // submit the form via Ajax
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            dataType: 'html',
            data: form.serialize(),
            success: function(result) {
                // Inject the result in the HTML
                $('#form-result').html(result);
            }
        });
    });
});
</script>

? Should it be located in the contact_form.html/twig after:

{% include "forms/form.html.twig" with {form: forms('contact_form')} %}

9 years ago

Hi
Yes after is correct,
Your form is created here:
{% include "forms/form.html.twig" with {form: forms('contact_form')} %}

The results than come back to (assuming you have a submission response):
<div id="form-result"></div>

And that JS <script> code does the actual submission.
If you want to auto scroll down after submission you'll need to add this bit back in

JS
   complete: function(result) {
        $('html, body').animate({
            scrollTop: $("#form-result").offset().top
        }, 2000);  

also if you're using a modular page you'll probably want this code right in the module template rather than with the rest of the JS just to make sure it is getting used.

👍 1
9 years ago

Not sure if I'm doing it right. Should I add it in my contact_form.html.twig file, so it looks like this?

HTML
    {% include "forms/form.html.twig" with {form: forms('contact_form')} %}

<div id="form-result"></div>

<script>
function(result) {
        $('html, body').animate({
            scrollTop: $("#form-result").offset().top
        }, 2000);
</script>
9 years ago

The order of your fields is right and should all go in your contact_form.html.twig as you mentioned.

If you want the form to submit, and then scroll down to that "form-result" div (where you would have some sort of confirmation message) then you would need everything from the <script></script> tag from my original post. Your reply cut out the part that scrolls down and now you've only got the scrolling bit left so its getting a little confusing. Also my original post left off the code blocks bits as those are theme specific.
This is the code from my actual page however your mileage will vary as I use bootstrap, this is from a page (not a module) and have a JavaScript block at the "bottom" which may or may not match your particular setup. Hopefully it helps

contact.html.twig

HTML
{% block content %}
 {{ parent() }}
<section class="section-gen">
<div class="container">
  <div class="row">
    <div class="col-xs-12">
      {% include "forms/form.html.twig" with { form: forms("contact-us") } %}
    </div>
  </div>
</div>
</section>

<section class="section-gen">
  <div class="container">
    <div class="row">
      <div class="col-xs-10 col-xs-offset-1">
        <div id="form-result"></div>
      </div>
    </div>
  </div>
</section>
{% endblock %}

{% block bottom %}
{{ parent() }}
<script>
$(document).ready(function(){
  var form = $("#contact-us");
  form.submit(function(e) {
      e.preventDefault();

      $.ajax({
          url: form.attr("action"),
          type: form.attr("method"),
          dataType: "html",
          data: form.serialize(),
          success: function(result) {
              $("#form-result").html(result);
          },
          complete: function(result) {
              $("html, body").animate({
                  scrollTop: $("#form-result").offset().top
              }, 2000);   
          }
      });
  });    
});
</script>
{% endblock %}
👍 2
last edited 11/23/17 by Jared
9 years ago

oops just noticed a bit of completely unrelated JS near the end that I've just deleted out.

5 years ago

You can set action: '#anchor'. Make sure to quote the anchor, otherwise it wont work! Each modular should have an id set by the theme, you can checkout the twig template for that or simply add an id yourself in your modular markdown file.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 132 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago