Recaptcha Validation in PHP

Asked

Viewed 192 times

1

I have a form and inside it a Recaptcha, originally from Google.

<form method="post" id="form-ligamos">
        <label>
            <input class="fields text" type="text" name="empresa" value="<?php echo $_POST['empresa']?>" placeholder="Digite o nome da sua empresa">
        </label>            
        <div class="captchaFix">
            <div class="g-recaptcha" data-sitekey="SECRET"></div>
        </div>
        <input class="btn-submit btn-submit-call" type="submit" name="fale" value="Solicitar ligação">
        <input class="btn-submit btn-submit-callFixIphone" type="submit" name="fale" value="Me Ligue">
    </form>

He’s inside the div captchaFix code-compliant. How do I make it validate whether or not the user clicked on it before the user goes to Submit?

1 answer

1


You can check the g-recaptcha-response in the Submit of your form.

function validaCaptcha() {
  if(document.querySelector('#g-recaptcha-response').value == '') {
     alert('Resolva o desafio do captcha para prosseguir!');
     return false;
  }
}

In your HTML it would look like this:

<form method="post" id="form-ligamos" onsubmit="return validaCaptcha();">
        <label>
            <input class="fields text" type="text" name="empresa" value="<?php echo $_POST['empresa']?>" placeholder="Digite o nome da sua empresa">
        </label>            
        <div class="captchaFix">
            <div class="g-recaptcha" data-sitekey="SECRET"></div>
        </div>
        <input class="btn-submit btn-submit-call" type="submit" name="fale" value="Solicitar ligação">
        <input class="btn-submit btn-submit-callFixIphone" type="submit" name="fale" value="Me Ligue">
    </form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.