Paste Value reCaptcha Google Ajax

0

I am registering a form with Ajax, how do I implement the use of reCaptcha?

<input type="text" name="email" id="email" class="form-control" placeholder="Email" required>
<div class="g-recaptcha" data-sitekey="bla,bla,bla"></div>
<input type="button" onclick="add_comment()" value="Enviar comentário" class="btn btn-primary pull-right"></button>

The function 'add_comment ()':

function add_comment() {
var url = 'comentarios/add_comment.php';
var method = 'POST';
params += '&email='+document.getElementById('email').value;
var container_id = 'comment-box' ;
var loading_text = '<img src="loader.gif">' ;
ajax (url, method, params, container_id, loading_text) ;
}

Setting an id in reCaptcha returns the value 'undefined' The function works perfectly with register, how can I send the value of reCaptcha to the function and validate it in the .php file?

Vlw

    
asked by anonymous 05.11.2015 / 17:18

1 answer

0

Assuming that the correct captcha value is in the "data-sitekey" attribute of the ".g-recaptcha" div

Maybe this will solve: (important: I'm using jquery, make sure you have included the jquery library)

<input type="text" name="email" id="email">
<input type="text" name="captcha" id="captcha">
<div class="g-recaptcha" data-sitekey="bla,bla,bla"></div>
<input type="button" value="CADASTRAR" onclick="submitForm()">

function submitForm() {
  if( $('#captcha').val() == $('.g-recaptcha').attr('data-sitekey') ) {
    $.ajax{
        url: 'comentarios/add_comment.php',
        type: 'POST',
        data: {
            email: $('#email').val()
        },
        success: function( retornoPHP ) {
            alert('enviou');
        }
    }
  } else {
      alert('o captcha está incorreto');
  }
}
    
07.11.2015 / 02:23