使用AJAX检查验证码验证

时间:2015-10-16 21:49:55

标签: php jquery ajax

所以这是我的验证码captcha_test函数,它当前在控制器中:

public function captcha_test(){

    // perform cehptcha check
    if (!CaptchaModel::checkCaptcha(Request::post('captcha'))) {
        Session::add('feedback_negative', Text::get('FEEDBACK_CAPTCHA_WRONG')); // No need for this line with AJAX
        Redirect::to('dashboard/test'); // No need for this line with AJAX
        return false;
    }else{
        Session::add('feedback_positive', 'Captcha is ookey!');
        Redirect::to('dashboard/test');
        return true;
    }

}

这就是形式:

<!-- <form method="post" action="<?php echo Config::get('URL'); ?>dashboard/captcha_test"> -->
<form>
   <!-- show the captcha by calling the login/showCaptcha-method in the src attribute of the img tag -->
            <img id="captcha" src="<?php echo Config::get('URL'); ?>login/showCaptcha" />
            <input type="text" name="captcha" placeholder="Please enter above characters" required />

            <!-- quick & dirty captcha reloader -->
            <a href="#" style="display: block; font-size: 11px; margin: 5px 0 15px 0; text-align: center"
               onclick="document.getElementById('captcha').src = '<?php echo Config::get('URL'); ?>login/showCaptcha?' + Math.random(); return false">Reload Captcha</a>

            <input type="button" onclick="ValidateCaptcha()" value="Testa Captcha!" />
</form>    

如何使用AJAX验证验证码(无页面重新加载)?如果无效,请重新加载CAPTCHA图像? login/showCaptcha将CAPTCHA短语写入会话。

到目前为止,我一直在谷歌搜索类似的东西,我发现这个stack thread关于同一主题。

所以我的AJAX现在看起来像这样:

function ValidateCaptcha()
{
var $true = false;
$.ajax(
{
    type: "POST",
    url: "<?php echo Config::get('URL'); ?>dashboard/captcha_test" + captcha,        
    data: {ptitle: captcha},
    success: function(response){
        if (response.d == "false") {
            captcha = 1;
            alert("Captcha is okey");
            $true = true;

        }else{
           captcha = 0;
           alert("Captcha is not okey, reload captcha and try again.");
        }

    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    },
    async: false
});

  if($true) return true;

  return false;
}

但导致insertAdjacentElementjquery-2.1.3.min.js:2684TypeError: Can only call HTMLElement.insertAdjacentElement on instances of HTMLElement错误。

0 个答案:

没有答案