boehmi
(Michael Boehm1988)
November 23, 2012, 2:34pm
1
Hey folks,
i want to use CCaptcha for my contact form, everything (rendering, validation, error-Messages etc.) works fine but the refresh-functionality
My first approach was to use set "showRefreshButton" to true, but when i load the page i got an JS Error
"ReferenceError: dummy is not defined
dummy"
This "dummy"-Code is registered in the function registerClientScript() of CCaptcha… wtf?
Then i tried to use the "clickableImage" method
If i click the image an Ajax Request is sent… the answer also looks pretty good, but i get a jQuery Error "Invalid Label" (see Screenshot)
I have other projects where everything works well (the Ajax answer looks similar)
Yii Version is 1.1.7
Does anyone have an idea what i could do wrong?
Thanks in advance
robregonm
(Ricobregon)
November 23, 2012, 2:36pm
2
Please try taking a look at SiteController… it will give some tips about how to implement captchas.
Regards.
boehmi
(Michael Boehm1988)
November 23, 2012, 2:38pm
3
I implemented it exactly like in the default Yii-application (i’m not using CCaptcha for the first time and never had problems like that)
As i said, everything besides the refresh works fine
robregonm
(Ricobregon)
November 23, 2012, 4:50pm
4
Interesting… Could you please check that the generated JS script for captcha, is ok?
Mine looks like this:
jQuery('#yw0').after("<a id=\"yw0_button\" href=\"\/index.php\/site\/captcha?refresh=1\">Get a new code<\/a>");
jQuery(document).on('click', '#yw0_button', function(){
jQuery.ajax({
url: "\/index.php\/myController\/captcha?refresh=1",
dataType: 'json',
cache: false,
success: function(data) {
jQuery('#yw0').attr('src', data['url']);
jQuery('body').data('captcha.hash', [data['hash1'], data['hash2']]);
}
});
return false;
});
boehmi
(Michael Boehm1988)
November 26, 2012, 9:05am
5
Hi,
mine looks like this:
jQuery(function($) {jQuery('#yw0').live('click',function(){
jQuery.ajax({
url: "\/contact\/default\/captcha\/refresh\/1\/",
dataType: 'json',
cache: false,
success: function(data) {
jQuery('#yw0').attr('src', data['url']);
jQuery('body').data('captcha.hash', [data['hash1'], data['hash2']]);
}
});
By debugging i discovered that jQuery is trying to eval the received JSON data instead of decoding it… ? Then it crashs with the “invalid label” error, because it’s no JS code ^^
I can’t use the refresh button, because of the “dummy” Code generated by CCaptchas registerClientScript():
...
if($this->showRefreshButton)
{
$cs->registerScript('Yii.CCaptcha#'.$id,'dummy');
...
boehmi
(Michael Boehm1988)
November 28, 2012, 4:33pm
6
I fixed it by changing the JavaScript in CCaptchas registerClientScript() to
jQuery(function($) {jQuery('#yw0').live('click',function(){
jQuery.ajax({
url: "\/contact\/default\/captcha\/refresh\/1\/",
dataType: 'text', // <--
cache: false,
success: function(data) {
data = jQuery.parseJSON(data); // <--
jQuery('#yw0').attr('src', data['url']);
jQuery('body').data('captcha.hash', [data['hash1'], data['hash2']]);
}
});
and commenting out the $cs->registerScript(‘Yii.CCaptcha#’.$id,‘dummy’); line
But i feel bad by changing the frameworks code