我有100.php與ajax調用200.php窗體。PHP谷歌reCAPTCHA與AJAX不起作用
<html>
<head>
<!-- include reCAPTCHA API JavaScript library given by Google -->
<script src='https://www.google.com/recaptcha/api.js'></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#btn").click(function(event){
event.preventDefault();
var myname = $("#name").val();
var myaddress = $("#address").val();
var yourData ='name='+myname+'&address='+myaddress; // php is expecting name and age
$.ajax({
type:'POST',
data:yourData,//Without serialized
url: '200.php',
success:function(data) {
if(data){
$('#testform')[0].reset();//reset the form
$('#result').html(data); // here html()
//alert('Submitted');
}else{
return false;
}
}
});
});
});
</script>
</head>
<body>
<form method="post" id="testform">
Name: <input type="text" name="name" value="" id="name"/> <br />
Address: <input type="text" name="address" value="" id="address"/>
<!--
place for the reCAPTCHA widget div with your site key
data-theme="dark" attribute - gives dark version
-->
<div class="g-recaptcha" data-sitekey="6LeJ8h8TAAAAAMS9nQX89XccpsC-SDeSycipiaHN"></div>
<input type="submit" name="ok" value="Send" id="btn"/>
</form>
<div id='result'></div>
</body>
200.php確實驗證驗證碼,進入diaplay名稱和地址經過用戶。但我的問題是,當我輸入姓名和地址時,點擊驗證碼。 Captha也被驗證並顯示在我的屏幕截圖中。但名稱和地址不顯示在頁面上。你也可以在這裏檢查一下自己:http://raveen.comlu.com/100.php
我是新來的PHP的Ajax調用。我GOOGLE了,我可以通過螢火蟲排除故障。你能說我在這裏做錯了嗎?以及如何檢查我的ajax調用是否完成等螢火蟲疑難解答的步驟?謝謝你的幫助。
注意:當我把所有這些代碼放在一個頁面中而不使用ajax調用。它工作正常!!!!!我想這種情況沒有頁面重載....
200.php
<?php
require_once('recaptchalib.php');
if(isset($_POST['ok'])){
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$secret = "6LeJ8h8TAAAAAB3IFQVQEaoApFe6lvq4Wxlktvn1"; //your secret key
$response = null; //empty response
$reCaptcha = new ReCaptcha($secret); //check secret key is present
$response = $reCaptcha->verifyResponse($_SERVER['REMOTE_ADDR'], $_POST['g-recaptcha-response']);
//$response variable will report back with "success"
if($response!=null && $response->success){
echo "<h1>Hi ". $_POST['name']. " from ". $_POST['address']. ", thanks for submitting the form!</h1>";
}
}
else{
echo "<h1>Please click on the reCAPTCHA box.</h1>";
}
}
?>
您使用的是Google recaptcha V2嗎? –
@RajdeepPaul,先生,我不知道。我怎麼知道的? –
我在下面給出了一個答案。希望這會解決你的問題。 –