T
T
testkeyc2020-02-11 09:14:41
JavaScript
testkeyc, 2020-02-11 09:14:41

How to fix an error when connecting captcha from google?

Connected captcha to the site, but the visitor can submit data from the form even when the verification is not passed. How to fix?

let form = document.querySelector('#form');

form.addEventListener('submit', function(evt) {
  evt.preventDefault();

    let formData = {
      name: document.querySelector('input[name="name"]').value,
      phone: document.querySelector('input[name="phone"]').value,
      email: document.querySelector('input[name="email"]').value,
      textarea: document.querySelector('textarea[name="textarea"]').value,
    };

    var request = new XMLHttpRequest();

    request.addEventListener('load', function() {
      alert('Ваша заявка успешно отправлена!');
      form.innerHTML = '<h2 class="form__height">Спасибо за заявку!</h2>';
    });

    request.open('POST', './mail.php', true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    request.send('name=' + encodeURIComponent(formData.name) + '&phone=' + encodeURIComponent(formData.phone) + '&email=' + encodeURIComponent(formData.email) + '&textarea=' + encodeURIComponent(formData.textarea) );
  });

 let onloadCallback = function() {
    alert("grecaptcha is ready!");
  };

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question