私はreCaptchaを私のウェブサイトの退院フォームに追加しようとしています。私はそれを行う方法についてのビデオチュートリアルに従ってきましたが、私はajaxを使用してPHPファイルを呼び出すフォームで実際にフォームを送信しないように、フォームに適合させるのに苦労しています。私は以前の質問で示唆したいくつかを試しましたが、意図した結果を得ず、代わりに「私はロボットが好きではありません」と表示して登録ページに表示します。あなたが何かを考えることができるなら、いくつかのヒント/示唆は素晴らしいでしょう。Ajax&Google reCaptcha
<div class="g-recaptcha" data-sitekey="6LcXMg0UAAAAABmlDlOGa6onxqqzERZ483XOJbFm"></div>
Javascriptを
function Register(){
var Forename = $("#txtForename").val();
var Surname = $("#txtSurname").val();
var Password = $("#txtPassword").val();
var PasswordR = $("#txtPasswordR").val();
var response = $("#g-recaptcha").val();
$.post('functions/php/fncregister.php', {Forename: Forename, Surname: Surname, Password: Password, PasswordR: PasswordR, response: response}, function(data) {
var returnValue = JSON.parse(data);
if (returnValue['data'] == 0){
$('#mdlInfo').html('<p>Your account has been created under the username: <strong><span id="spnUsername">'+returnValue['username']+'</span></strong>. You <strong>must</strong> remember this as you will require it to log into your account.</p><p>Your account has also been added to a moderation que. <strong>You must wait until a member of staff activates your account!</strong></p>');
$("#mdlRegister").modal("show");
}
else if (returnValue['data'] == 1){
$('#divError').html('<p class="text-center text-danger bg-danger" id="pUPInc">Passwords did not match!</p>');
}
else if (returnValue['data'] == 3){
$('#divError').html('<p class="text-center text-danger bg-danger" id="pUPInc">An error occured when adding your details to the Database!</p>');
}
else if (returnValue['data'] == 4){
$('#divError').html('<p class="text-center text-danger bg-danger" id="pUPInc">I don\'t like Robots!</p>');
}
});
}
PHPここ
<?php
//Retrieves variables from Javascript.
$Forename = $_POST["Forename"];
$Surname = $_POST["Surname"];
$Password = $_POST["Password"];
$PasswordR = $_POST["PasswordR"];
//reCaptcha
$Url = "https://www.google.com/recaptcha/api/siteverify";
$SecretKey = "---KEY---";
$Response = file_get_contents($Url."?secret=".$SecretKey."&response=".$_POST['response']."remoteip=".$_SERVER['REMOTE_ADDR']);
$Robot = json_decode($response);
$data = 0;
if(isset($Robot->success) AND $Robot->success==true){
//OTHER CODE
}
else{
//This code always runs (though this is only meant to happen if reCaptcha detects a robot.
$data = 4;
echo json_encode(["data"=>"$data"]);
?>
私のテストでは実際に私の実際のキーを使っていましたが、セキュリティ上の理由から私はここでそれを削除しました。 –
コードを少なくするために、この例を煮詰めることをお勧めします。確かに、この問題は、複製または理解するための完全なコードレビューを行うものではありません。例がより臨床的で、より少ない場合は、より専門的な助言を得るでしょう。 – WEBjuju
@WEBjuju、yehのコードは手元の質問のために少し上にあった。私はそれを煮詰めましたので、うまくいけばそれは理解しやすくなります。 –