Captcha在jquery方面没有validation..!

我正在使用PHP来validation谷歌REcaptcha ..至于后端validationgose工作正常它如果validationvalidation码没有提交错误,如果完成用户存储在数据库中,但当我用jquery连接它时出现重大问题。即使后端validationvalidation码,每次都会发出错误“你遗失的validation码”的错误,请帮助我解决这个问题,请原谅我,如果我错了.. !!

这里是gose .php文件

 false); if (!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['mobile'])){ /* if required include seperate validation */ // receiving the post params $fname = trim($_POST['fname']); $lname = trim($_POST['lname']); $email = trim($_POST['email']); $password = $_POST['password']; $mobile = trim($_POST['mobile']); /* validation process starts from here */ // validate your email address if(filter_var($email, FILTER_VALIDATE_EMAIL)) { //validate your password if(strlen($password) >= 6){ //validate your mobile if(strlen($mobile) == 12){ //validate captcha //your site secret key $secret = 'XXXX_secret-key_XXXX'; if(isset($_POST['recaptchaResponse']) && !empty($_POST['recaptchaResponse'])){ //get verified response data $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['recaptchaResponse']; $verifyResponse = file_get_contents($param); $responseData = json_decode($verifyResponse); if($responseData->success){ //Check for valid email address if ($db->isUserExisted($email)) { // user already existed $response["error"] = true; $response["error_msg"] = "User already existed with " . $email; echo json_encode($response); }elseif($db->isMobileNumberExisted($mobile)) { //user already existed $response["error"] = true; $response["error_msg"] = "user already existed with" . $mobile; echo json_encode($response); }else{ // create a new user $user = $db->storeUser($fname, $lname, $email, $password, $mobile); if ($user) { // user stored successfully $response["error"] = false; $response["uid"] = $user["id"]; $response["user"]["fname"] = $user["fname"]; $response["user"]["lname"] = $user["lname"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed to store $response["error"] = true; $response["error_msg"] = "Unknown error occurred in registration!"; echo json_encode($response); } } }else{ //failed to submit captcha $response["error"] = true; $response["error_msg"] = "Sorry this application is not for bots"; echo json_encode($response); } }else{ //failed to submit captcha $response["error"] = true; $response["error_msg"] = "your missing captcha"; echo json_encode($response); } }else{ //invalid mobile number $response["error"] = true; $response["error_msg"] = "Mobile number is invalid!"; echo json_encode($response); } }else{ //min of 6-charecters $response["error"] = true; $response["error_msg"] = "password must be of atleast 6-characters!"; echo json_encode($response); } }else{ // invalid email address $response["error"] = true; $response["error_msg"] = "invalid email address"; echo json_encode($response); } }else{ //missing the required fields $response["error"] = true; $response["error_msg"] = "Please fill all the required parameters!"; echo json_encode($response); } ?> 

这里是引导程序中的gh .html文件

       MiiSKy | Register            
<!--

MiiSky

-->
<!--

Register to MiiSky

-->

Create account to see it in action.

Already have an account?

Login

这里是主要的.js文件

  $(document).ready(function(){ //execute's the function on click $("#submit").click(function(e){ var recaptchaResponse = grecaptcha.getResponse(); var status = $('form')[0].checkValidity(); if(status){ /*jquery to call the url requested and parse the data in json*/ $.ajax({ url: "process.php", type: "POST", data: { fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val(), password: $("#password").val(), mobile: $("#mobile").val(), recaptchaResponse: recaptchaResponse }, async: false, dataType: "JSON", /*Give out the alert box to display the results*/ success: function (json){ if(json.error){ alert(json.error_msg); grecaptcha.reset(); e.preventDefault(); }else{ alert("Registeration successful!",json.user.email); $('#register').submit(); } }, error: function(jqXHR, textStatus, errorThrown){ alert(errorThrown); } }); } }); }); 

首先确保您已包含必要的JavaScript资源以正确呈现reCAPTCHA小部件,如下所示:

   reCAPTCHA demo: Simple page    

这是参考:

  • 显示小部件

现在来看你的用户的回复。 用户的validation码挑战的响应可以通过三种方式获取。 它可以是,

  • g-recaptcha-response – 提交表单中的POST参数
  • grecaptcha.getResponse(widget_id) – 将在用户完成validation码后提供响应。
  • 传递给render方法的config对象中指定的回调函数的字符串参数。

这是参考:

  • validation用户的响应

为了您的目的,使用grecaptcha.getResponse()来获取用户的响应。

并且作为旁注使用grecaptcha.reset()来要求最终用户再次使用reCAPTCHA进行validation。 从手册:

如果您的网站使用AJAX请求执行服务器端validation,则只应validation用户的reCAPTCHA响应令牌( g-recaptcha-response )一次。 如果使用特定令牌进行了validation尝试,则无法再次使用它。 您需要调用grecaptcha.reset()来要求最终用户再次使用reCAPTCHA进行validation。

这是您的HTML代码:

 

Already have an account?

Login

你的jQuery应该是这样的:

 $(document).ready(function(){ //execute's the function on click $("#submit").click(function(e){ var recaptchaResponse = grecaptcha.getResponse(); var status = $('form')[0].checkValidity(); if(status){ /*jquery to call the url requested and parse the data in json*/ $.ajax({ url: "process.php", type: "POST", data: { fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val(), password: $("#password").val(), mobile: $("#mobile").val(), recaptchaResponse: recaptchaResponse }, async: false, dataType: "JSON", /*Give out the alert box to display the results*/ success: function (json){ if(json.error){ alert(json.error_msg); grecaptcha.reset(); e.preventDefault(); }else{ alert("Registeration successful!",json.user.email); $('#register').submit(); } }, error: function(jqXHR, textStatus, errorThrown){ alert(errorThrown); } }); } }); }); 

最后你的PHP应该是这样的:

 success){ // success }else{ // failure } } // your code ?>