2

I made an Ajax Form, But before some days, I added Invisible Google Captcha. But now, the Google Captcha code, re-direct the user to form action URL.

This is my code before adding Captcha:

$("form.formajax").submit(function(e) {

  e.preventDefault();

  var data = $(this).serialize();
  var url = $(this).attr("action");
  var form = $(this); // Add this line
  
  $.post(url, data, function(data) {
  $(form).children(".signupresult").html(data.signupresult);

  });
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<form class="formajax" method="post" action="admin/login/logincheck.php">

  <input type="text" placeholder="username">
  <input type="submit" />

</form>

And Here is my code after adding Captcha

function onSubmit(token) {
  document.getElementById("i-recaptcha").submit();
}

$("form.formajax").submit(function(e) {

  e.preventDefault();
  var data = $(this).serialize();
  var url = $(this).attr("action");
  var form = $(this); // Add this line

  $.post(url, data, function(data) {
    $(form).children(".signupresult").html(data.signupresult);
  });
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script src='https://www.google.com/recaptcha/api.js'></script>

<form id='i-recaptcha' class="formajax" method="post" action="admin/login/logincheck.php">
  <input type="text" placeholder="username">
  <input type="submit" class="g-recaptcha" data-sitekey="6Lcjvi4UAAAAAIR7_ckItF2HfMEFHx427WUDdGDk" data-callback="onSubmit" />

</form>

Probably the onSubmit function, is causing the problem. How can i make this form ajax?

Toby
  • 717
  • 2
  • 8
  • 19

0 Answers0