0

I am trying to implement reCaptcha on a webpage, everything is working fine but I am not able to validate if the user really successfully passed the captcha or not. The code is working but when I click the button manually, then it shows error. I was trying to make the form get submitted automatically once the user fills the captcha.

<script>
    $(document).ready(function() {
          $('#myform').click(function(event) {
              
                var recaptcha_secret = "secret_key";
                var recaptcha_response = $('#g-recaptcha-response').val();
                var url = "https://www.google.com/recaptcha/api/siteverify";
                var data = {
                  secret: recaptcha_secret,
                  response: recaptcha_response
                };
                $.ajax({
                    url:url,
                    method:'POST',
                    data:data,
                    success: function(response){
                        if(response.success){
                            window.location.href="main.jsp";
                        }
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        alert("Error:"+ textStatus+ errorThrown);
                      }
                });
          });
        });
</script>

This is all the jQuery code.

<body>

    <form method="POST" action="#" id="myform">
      <div class="g-recaptcha" data-sitekey="site_key"></div>
      <br/>
      <button type="submit">Submit</button>
    </form>
    
</body>

This is My HTML code.

I want to redirect the user once response.success==true to another page.

mvalpha
  • 38
  • 5

0 Answers0