我想在我的验证码图像旁边有一个重新加载按钮,以便在 codeigniter 中使用 jquery 重新加载它。我在网上搜索以找到解决方案,但我发现的一切都让我感到困惑。
这是我的控制器:
function create_captcha()
{
$expiration = time()-300; // Two hour limit
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
$vals = array(
//'word' => 'Random word',
'word_length' => 4,
'img_path' => './uploads/captcha/',
'img_url' => base_url().'uploads/captcha/',
'font_path' => './system/fonts/texb.ttf',
'img_width' => '110',
'img_height' => '30',
'expiration' => '3600'
);
$cap = create_captcha($vals);
//puts in the db
$captchadata = array(
'captcha_id' => '',
'captcha_time' => $cap['time'],
'ip_address' => $this->input->ip_address(),
'word' => $cap['word']
);
$query = $this->db->insert_string('captcha', $captchadata);
$this->db->query($query);
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') echo $cap['image'];
else return $cap['image'];
这是我的观点:
<div class="captcha-area">
<? echo form_input('captcha', '', 'class="field text captcha"')?>
<div id="cap-img">
<? echo $image;?>
</div>
<a title="reload" class="reload-captcha" href="#"><img src="<? echo base_url(); ?>images/reload.png" /></a>
<div class="clear"></div>
</div>
<script>
$(function(){
var base_url = '<?php echo base_url(); ?>';
$('.reload-captcha').click(function(event){
event.preventDefault();
$('.captcha-img').attr('src', base_url+'dashboard/create_captcha?'+Math.random());
});
});
</script>
编辑:
这是加载网站后的源代码
<div class="captcha-area">
<input type="text" name="captcha" value="" class="field text captcha">
<div id="cap-img">
<img src="http://example.com/uploads/captcha/1382346264.1026.jpg" width="110" height="30" class="captcha-img" style="border:0;" alt=" ">
</div>
<a title="reload" class="reload-captcha" href="#"><img src="http://example.com/images/reload.png"></a>
<div class="clear"></div>
</div>
当我点击重新加载按钮时,它会将 src 属性更改为:
<img src="http://example.com/dashboard/create_captcha?0.8049291325733066" width="110" height="30" class="captcha-img" style="border:0;" alt=" ">