2011-08-11 72 views
0

我使用jquery和codeigniter创建catcha。我的问题是刷新验证码图像src不刷新。使用jquery和codeigniter刷新图像src

下面是创建验证码的代码

<?php 

类Get_captcha延伸,用于HTML控制器 {

function Get_captcha() 
{ 
    parent::controller(); 
    $this->load->library('session'); 
    $this->load->library('form_validation');  
    $this->load->helper(array('form', 'url')); 
    $this->load->model('tools_model');      
} 

function index($random = false) 
{ 
    $rand_string = $this->tools_model->RandomString(5);  
    $new_sess_data = array("random_number"=>$rand_string); 
    $this->session->set_userdata($new_sess_data);  

    $image = imagecreatetruecolor(148, 25);  
    $dir = 'fonts/'; 

    $num = rand(1,2); 
    if($num==1) 
    { 
     $font = "Capture it 2.ttf"; // font style 
    } 
    else 
    { 
     $font = "Molot.otf";// font style 
    } 

    // random number 1 or 2 
    $num2 = rand(1,2); 
    if($num2==1) 
    { 
     $color = imagecolorallocate($image, 113, 193, 217);// color 
    } 
    else 
    { 
     $color = imagecolorallocate($image, 163, 197, 82);// color 
    } 

    $white = imagecolorallocate($image, 255, 255, 255); // background color white 
    imagefilledrectangle($image,0,0,399,99,$white); 

    imagettftext ($image, 18, 0, 38, 22, $color, $dir.$font, $this->session->userdata('random_number')); 

    header("Content-type: image/png"); 
    imagepng($image); 
} 
} 

代码

<img src="<?php echo site_url('get_captcha') ?>" alt="" id="captcha" /><a href="#" class="b" id="refresh-captcha">Refresh</a> 

代码的jquery

$('#refresh-captcha').click(function(){ 
    $('#captcha').attr('src','<?php echo site_url('get_captcha') ?>'); 
}); 

回答

0

看起来你并没有真正改变图像。您需要更改浏览器的URL以再次查找图像。试着改变你的JS代码看起来像这样:

$('#refresh-captcha').click(function(){ 
    var random = new Date().getTime(); 
    $('#captcha').attr('src','<?php echo site_url('get_captcha') ?>?random='+random); 
}); 

这将迫使形象是“不同的”每按一次按钮。

+0

嗨,我试过你的代码,但没有图像将显示。当我看着萤火虫“无法加载给定的网址”,谢谢 – tirso

+0

那么,它试图加载的URL是什么?它有效吗?你是否尝试过手动加载它? site_url()是否输出有效的完全限定的URL? – OverZealous