Skip to content

Instantly share code, notes, and snippets.

@myasseen
Forked from madan712/test.php
Created February 2, 2016 22:49
Show Gist options
  • Save myasseen/0e30f341634f7bfb57fe to your computer and use it in GitHub Desktop.
Save myasseen/0e30f341634f7bfb57fe to your computer and use it in GitHub Desktop.
PHP example - Google Recaptcha validation using jquery (AJAX)
<?php
$remoteip = $_SERVER['REMOTE_ADDR'];
?>
<!doctype html>
<html>
<head>
<title> Recaptcha validation using jquery ajax </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="recaptcha_ajax.js"></script>
<script type="text/javascript">
function reloadRecaptcha() {
var publicKey = "your_public_key";
var div = "recap";
Recaptcha.create(publicKey,div,{theme: "white"});
return false;
}
function validate() {
var challenge = Recaptcha.get_challenge();
var response = Recaptcha.get_response();
var remoteip = "<?php echo $remoteip; ?>";
$.ajax({
type: "POST",
url: "validateRecaptcha.php",
async: false,
data: {
remoteip: remoteip,
challenge: challenge,
response: response
},
success: function(resp) {
if(resp == "true") {
document.getElementById("message").innerHTML = "Perfect!";
}
else {
document.getElementById("message").innerHTML = "Incorrect Recaptcha! Please try again!";
reloadRecaptcha();
}
}
});
return false;
}
</script>
</head>
<body onload="return reloadRecaptcha();">
<form method="post" onsubmit="return validate();">
<table>
<tr>
<td colspan="2">
<div id="message" style="color:#ff0000; "></div>
<div id="recap"></div>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment