简体   繁体   English

荷兰芹远程重新验证

[英]Parsley Remote Recaptcha Validation

I implemented a multistep form using parsley and I need to validate a recaptcha. 我使用欧芹实现了一个多步骤表单,我需要验证一个Recaptcha。 The first problem is that the recaptcha is generated by the function recaptcha_get_html($publickey) , so I cannot add attributes manually within the input element. 第一个问题是recaptcha是由函数recaptcha_get_html($ publickey)生成的 ,因此我无法在输入元素中手动添加属性。 Nonetheless I solved that using .attr() on window.onload() as follows: 尽管如此,我解决了在window.onload()上使用.attr()的问题,如下所示:

        window.onload = function() {
            $("#recaptcha_response_field").attr('data-parsley-group','block1');
            $("#recaptcha_response_field").attr('data-parsley-required','true');
            $("#recaptcha_response_field").attr('data-parsley-remote-message','Wrong Captcha');
            $("#recaptcha_response_field").attr('data-parsley-remote','lib/validateRecaptcha.php');

        };

To accomplish the validation I need to pass to the php file two fields: recaptchaChallengeField and recaptchaResponseField. 要完成验证,我需要将两个字段传递给php文件:recaptchaChallengeField和recaptchaResponseField。

As I said, it is a multi step form... so to validate the form I have this logic attached to the "next" button, and I added the data-parsley-remote-options as follows: 正如我所说的,这是一个多步骤表单...因此,为了验证表单,我将此逻辑附加到了“下一步”按钮上,然后添加了data-parsley-remote-options,如下所示:

$('.next').on('click', function () {

      var current = $(this).data('currentBlock'),
      next = $(this).data('nextBlock');

      $("#recaptcha_response_field").attr('data-parsley-remote-options','{"type": "POST", "dataType": "jsonp", "data": {"recaptchaChallengeField": "'+$("#recaptcha_challenge_field").val()+'", "recaptchaResponseField": "'+$("#recaptcha_response_field").val()+'"}}');


      // only validate going forward. If current group is invalid, do not go further
      // .parsley().validate() returns validation result AND show errors
      if (next > current)
        if (false === $('#signup-form').parsley().validate('block' + current))
          return;

// validation was ok. We can go on next step.
      $('.block' + current)
        .removeClass('show')
        .addClass('hidden');

      $('.block' + next)
        .removeClass('hidden')
        .addClass('show');
    });

As you can see, the recaptchaResponseField is dynamic... I catch the value everytime the user press the "next" button. 如您所见,recaptchaResponseField是动态的...每当用户按下“下一步”按钮时,我都会捕获该值。

Here is my php code: 这是我的PHP代码:

require_once('recaptcha-php-1.11/recaptchalib.php');
          $privatekey = "private_key";
          $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptchaChallengeField"], $_POST["recaptchaResponseField"]);

          if (!$resp->is_valid)
            echo json_encode("404");
          else
            echo  json_encode("200");

The problem is that its not doing anything... I dont get any error message when typing something within the recaptcha input. 问题是它什么也没做...在recaptcha输入中键入内容时,我没有收到任何错误消息。 What can be the problem? 可能是什么问题? I already tried using parsley.remote.js (.addAsyncValidator) but nothing happens aswell... I need some help please. 我已经尝试过使用parsley.remote.js(.addAsyncValidator),但也没有任何反应...我需要一些帮助。 Thx 谢谢

I think that your problem is linked to the CAPTCHA generation. 我认为您的问题与验证码一代有关。 The following lines do not work: 以下行不起作用:

$("#recaptcha_response_field").attr('data-parsley-group','block1');
$("#recaptcha_response_field").attr('data-parsley-required','true');
$("#recaptcha_response_field").attr('data-parsley-remote-message','Wrong Captcha');
$("#recaptcha_response_field").attr('data-parsley-remote','lib/validateRecaptcha.php');

These attributes are not associated to the #recaptcha_response_field because the field is created after these calls. 这些属性未与#recaptcha_response_field关联,因为该字段是在这些调用之后创建的。 You can check in the page source code. 您可以签入页面源代码。

One option is to define a new custom theme in order to avoid automated generation of the HTML code. 一种选择是定义一个新的自定义主题,以避免自动生成HTML代码。 https://developers.google.com/recaptcha/docs/customization?hl=fr https://developers.google.com/recaptcha/docs/customization?hl=fr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM