/ Published in: JavaScript
A simple JSON request to validate a captcha control (using the php plugin provided by Recaptcha)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * author : Stephane P. Pericat * date : 2010-03-01 */ PHP Code: <?php require "recaptcha/recaptchalib.php"; if($_POST) { $resp = recaptcha_check_answer( "6LdrkwsAAAAAAHX2b45bn_...", //put your recaptcha private key here $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"] ); if ($resp->is_valid) { echo json_encode(true); } else { echo json_encode(false); } } ?> //----------------------------------------------- Javascript front-end: new Asset.javascript('http://api.recaptcha.net/js/recaptcha_ajax.js'); $(document.body).addEvent('submit', function(event) { event.stop(); var challenge = Recaptcha.get_challenge(); var response = Recaptcha.get_response(); var control = new Request.JSON({ url: 'control.php', data : { recaptcha_challenge_field : challenge, recaptcha_response_field : response }, method: 'post', onComplete : function (result) { if(result == true) { Recaptcha.destroy(); console.log(result); } else { Recaptcha.reload(); } } }).send(); });