重新加载Captcha不适用于jquery PHP

时间:2014-02-11 18:07:24

标签: php jquery magento captcha

我在magento中使用jquery php captcha.Captcha生成正常,但我也想刷新它但没有得到如何做。这是我使用的代码。

HTML代码

    <span id="reload_captcha_span"><img id="captcha_image" src="<?php echo $this->getUrl('helloworld/index/captcha'); ?>"/></span>
<span id="reload_captcha">reload</span>

JQuery代码

jQuery('#reload_captcha').click(function(){
                jQuery.ajax({
                        url: '<?php echo $this->getUrl('helloworld/index/captcha'); ?>',
                        success:function(data){
                            jQuery('#reload_captcha_span').html("");
                            jQuery('#reload_captcha_span').html(data);
                        }
                    }); 
            });

PHP代码

session_start();
        $ranStr = md5(microtime());
        $ranStr = substr($ranStr, 0, 6);
        $_SESSION['cap_code'] = $ranStr;
        $newImage = imagecreatefromjpeg("cap_bg.jpg");
        $txtColor = imagecolorallocate($newImage, 0, 0, 0);
        imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
        header("Content-type: image/jpeg");
        imagejpeg($newImage);

作为回应,我得到的未知数据可能就是图像本身。

请帮帮我。

提前致谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

jQuery('#reload_captcha').click(function(){
    var d = new Date();
    jQuery('#captcha_image').attr('src', '<?=$this->getUrl('helloworld/index/captcha')?>?'+d.getTime());
});

要获取相同的图片网址,但我们添加了一个随机(时间)参数,因此不会从缓存中提取。