Validation of ReCaptcha in PHP

1

I have a form and inside it a ReCaptcha, originating 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>

It's inside the div captchaFix as per code. How do I validate whether or not the user has clicked on it before the user went to submit ?

    
asked by anonymous 26.06.2017 / 21:00

1 answer

1

You can check the g-recaptcha-response on 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>
    
26.06.2017 / 21:06