0

I'm using Ajax to display a comments widget on my site and am trying to upgrade from Recaptcha v2 to Recaptcha v3. So far it's gone well, comments successfully post, but instead of displaying the "thanks for submitting" modal, it just redirects to the form submission URL with the success data. This is not ideal.

The main change I made was to change the button code in my comment_form.html to this:

<button class="button g-recaptcha" id="comment-form-submit"
    data-sitekey="{{ site.reCaptcha.siteKey }}"
    data-callback='onSubmit'
    data-action='submit'>
  Submit
</button>

</form>

<script>
  function onSubmit(token) {
    document.getElementById("new-comment").submit();
  }
</script>

(and added the id="new-comment" to the top of the form)

previously i had

<button class="button" id="comment-form-submit">Submit</button>

the relevant javascript code is:

(function ($) {
  var $comments = $('.js-comments');

  $('.js-form').submit(function () {
    var form = this;


    $("#comment-form-submit").html(
      '<svg class="icon spin"><use xlink:href="#icon-loading"></use></svg> Sending...'
    );
    $(form).addClass('disabled');

    $.ajax({
      type: $(this).attr('method'),
      url:  $(this).attr('action'),
      data: $(this).serialize(),
      contentType: 'application/x-www-form-urlencoded',
      success: function (data) {
        showModal('Comment submitted', 'Thanks! Your comment is <a href="https://github.com/datapolitical/chrisfnicholson.github.io/pulls">pending</a>. It will appear when approved.');

        $("#comment-form-submit")
          .html("Submit");

        $(form)[0].reset();
        $(form).removeClass('disabled');
        grecaptcha.reset();
      },
      error: function (err) {
        console.log(err);
        var ecode = (err.responseJSON || {}).errorCode || "unknown";
        showModal('Error', 'An error occured.<br>[' + ecode + ']');
        $("#comment-form-submit").html("Submit")
        $(form).removeClass('disabled');
        grecaptcha.reset();
      }
    });
    return false;
  });

I'm pretty sure it's like a one line change to make the Ajax process the reply, but I'm totally out of my depth with javascript, so any thoughts are greatly appreciated.

--

Edit: The other example I saw calls the onSubmit function from their callback but since I'm using this weird main.js I don't know how to reference $('.js-form').submit(function (event) { from onSubmit

1 Answer 1

0

The default browser behaviour after submitting a form is to redirect on success, try preventing this by calling preventDefault on the event, eg:

$('.js-form').submit(function (event) {
    event.preventDefault();
    // the rest of your code
});
Sign up to request clarification or add additional context in comments.

5 Comments

The submit event handler in OP's code returns false at the end. You're correct, but it's essentially doing the same (or at least supposed to).
Yea it works fine with recaptcha v2 so I'm assuming it handled that issue, but I'll give it a shot regardless.
Sadly you're correct @icecub that did not fix the problem.
The other examples I've seen call the onSubmit function from their callback but since I'm using this weird main.js I don't know how to reference $('.js-form').submit(function (event) { from onSubmit
@cfn Had a deeper look, and I noticed this $("#comment-form-submit").html("Submit");, I suspect this might be triggering the form submitsion, and thus the subsequent redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.