15

I have two radio buttons. When I click into one of them to change the form fields, the captcha version 1 does not show anymore.

So, I have to click the refresh button to generate a new captcha image.

<input type="radio" name="data[Form][sv]" id="FormV1" value="1" /> V1
<input type="radio" name="data[Form][sv]" id="FormV2" value="2" checked="checked" /> V2

The jquery code is:

$(document).ready(function () {
    $("#FormV1").bind("change", function (event) {
        $.ajax({async:true, 
        beforeSend:function (XMLHttpRequest) {
            $('#loading').show();
        }, 
        complete:function (XMLHttpRequest, textStatus) {$('#loading').hide()}, 
        data:$("#FormularioSituacaoVeiculo1").closest("form").serialize(), 
        dataType:"html", 
        evalScripts:true, 

success:function (data, textStatus) {
    $("#search").html(data);}, 
    type:"post", 
    url:"\/mysite\/theurl\/"});
    return false;
});

$("#FormV2").bind("change", function (event) {
    $.ajax({async:true, 
    beforeSend:function (XMLHttpRequest) {
    $('#loading').show();
}, 
    complete:function (XMLHttpRequest, textStatus) {
        $('#loading').hide()
    }, 
    data:$("#FormV2").closest("form").serialize(), 
    dataType:"html", evalScripts:true, success:function (data, textStatus) {
        $("#search").html(data);
    }, 
    type:"post", 
    url:"\/mysite\/url\/"});
    return false;
});

function showRecaptcha(element) {
    Recaptcha.create('hjfsdjklsdjklfsdjklfsdjklfjksdl', element, {
        lang : 'en-gb',
        theme : 'custom',
        custom_theme_widget: 'recaptcha_widget',
        callback: Recaptcha.focus_response_field
    }
);
}

showRecaptcha('recaptcha_div');

How can I switch the form fields (V1 to V2) and generate the captcha automatically without having to click on the refresh button?

Today the captcha is not generated when I do click on the radio button. So I have to click on the refresh button to regenerate a captcha image.

codewitharefin
  • 1,398
  • 15
  • 24
Ângelo Rigo
  • 2,039
  • 9
  • 38
  • 69
  • can you create a JSFiddle to check it? Thanks. – Gerard Cuadras Jun 29 '16 at 11:34
  • Do you have any errors in your console? Have you debugged it at all? – krillgar Jun 29 '16 at 11:54
  • Also you should probably post the html you're using, or just add it to the fiddle per @GerardCuadras – n_i_c_k Jun 29 '16 at 11:56
  • Following the documentation of google recaptcha (https://developers.google.com/recaptcha/docs/display) you can reset the recaptcha by "grecaptcha.reset()" - but the recaptcha described there looks different to what you posted - can you please provide more information about the used recaptcha - and in ideal world a jsfiddle? – Martin Ackermann Jun 29 '16 at 12:16
  • Hi i do provide a fiddle with the generated HTML into https://jsfiddle.net/9fngba56/ it is not a full working fiddle but you can see the structure aplyed – Ângelo Rigo Jun 29 '16 at 12:21
  • I am using Recaptcha version 1 so i can use Recaptcha.reload(); my problem is to identify where . i have created inside the success section of the bind callback, but it did not work until now. – Ângelo Rigo Jun 29 '16 at 12:22
  • after adding success=>Recaptcha.reload i do get the error at the console – Ângelo Rigo Jun 29 '16 at 12:35
  • :recaptcha_ajax.js?_=1467203673399:186 Uncaught TypeError: Cannot set property 'value' of null_set_challenge_internal @ recaptcha_ajax.js?_=1467203673399:186_set_challenge @ recaptcha_ajax.js?_=1467203673399:185finish_reload @ recaptcha_ajax.js?_=1467203673399:185(anonymous function) @ reload?c=03AHJ_VuuJrxsdfsdfsdfHSoQfdgfdgfdgfdgQOENvfo_DURctiESpWkmvqX7rh8XGxgVAsi6lNOreB3J6nVJqN60nS4…:1 – Ângelo Rigo Jun 29 '16 at 12:36
  • recaptcha_ajax.js?_=1467203673399:177 Uncaught TypeError: Cannot set property 'innerHTML' of null_init_builtin_theme @ recaptcha_ajax.js?_=1467203673399:177_finish_widget @ recaptcha_ajax.js?_=1467203673399:180challenge_callback_internal @ recaptcha_ajax.js?_=1467203673399:176Zb @ recaptcha_ajax.js?_=1467203673399:122Yb @ recaptcha_ajax.js?_=1467203673399:120N.callback @ recaptcha_ajax.js?_=1467203673399:120d.onload.d.onreadystatechange @ recaptcha_ajax.js?_=1467203673399:124 – Ângelo Rigo Jun 29 '16 at 12:36
  • The caught TypeError: Cannot set property 'innerHTML' of null error takes to these lines : c=c.replace(/IMGROOT/g,e);Z._set_style(c);Z.update_widget();Z.widget.innerHTML='
    '+d+"
    ";c=Z.getLang_();
    – Ângelo Rigo Jun 29 '16 at 13:08

2 Answers2

11

Please check this working example (I have tried my best to make this example as close as possible to your situation):

$('input[name="data[Form][sv]"]').change(function(e) {
  $.ajax({
    async: true,
    beforeSend: function(XMLHttpRequest) {
      $('#loading').show();
    },
    complete: function(XMLHttpRequest, textStatus) {
      $('#loading').hide()
    },
    data: $(this).closest("form").serialize(),
    evalScripts: true,
    success: function(data, textStatus) {
      $("#search").html(data);
      Recaptcha.reload();
    },
    dataType: "html",
    type: "POST",
    url: "https://httpbin.org/post"
  });
  return false;
});

function showRecaptcha(element) {
  Recaptcha.create('6Ld3TPkSAAAAAPckREYZuVQ6GtjMQsD-Q5CkzNxj', element, {
    lang: 'pt-BR',
    theme: 'white',
    custom_theme_widget: 'recaptcha_widget',
    //callback: Recaptcha.focus_response_field
  });
}

$(function() {
  showRecaptcha('recaptcha');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
<div id="recaptcha"></div>

<h4>Radio Inputs: </h4>
<form>
  <input type="radio" name="data[Form][sv]" id="FormV1" value="1" />V1
  <input type="radio" name="data[Form][sv]" id="FormV2" value="2" checked="checked" />V2
</form>
<h4>Ajax Post Result <span style="display:none" id="loading">Loading...</span>: </h4>
<div id="search">

</div>
max
  • 8,632
  • 3
  • 21
  • 55
Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
1

Refresh codes are what you are looking for?

for v1:

Recaptcha.reload(); 

for v2:

grecaptcha.reset();
ErTR
  • 863
  • 1
  • 14
  • 37
  • I found that even clicking on the default reload captcha button, wich calls Recaptcha.reload(), the image does not appear, i am starting to thinking about setup Recaptcha from zero, instead of hunting the bug. – Ângelo Rigo Jun 29 '16 at 16:44
  • I try to call Recaptcha.reload(); on success but did not reaload the image as expected. – Ângelo Rigo Jun 29 '16 at 16:45
  • One more thing i notice, is that clicking several times alternanting the two options the captcha does appear!!!! for the two diferente options – Ângelo Rigo Jun 29 '16 at 17:06