刷新验证码图像jquery codeigniter

时间:2017-08-11 01:03:40

标签: javascript php jquery codeigniter web

我的刷新验证码按钮有问题,在我重新加载页面后单击按钮时它只运行一次。我不知道我是错了还是错过了其他的东西。

我尝试使用jquery刷新验证码,因为它更有效。

这是我的代码:

控制器

public function refresh_login(){
    // Captcha configuration
    $config = array(
        'img_path'      => 'captcha_images/',
        'img_url'       => base_url().'captcha_images/',
        'img_width'     => '100',
        'img_height'    => '30',
        'word_length'   => '4',
        'font_size'     => '16'
    );
    $captcha = create_captcha($config);

    // Unset previous captcha and store new captcha word
    $this->session->unset_userdata('captchaCode');
    $this->session->set_userdata('captchaCode',$captcha['word']);

    // Display captcha image
    echo $captcha['image'];
}

jquery

$( document ).ready( function () {
  var base_url = '<?php echo base_url(); ?>';
     $('.reload-captcha').click(function(event){
         event.preventDefault();
         $.ajax({
             url:base_url+'index.php/auth/refresh_login',
             success:function(data){
                 $('.captcha-img').replaceWith(data);
             }
         });            
      });

视图

<form action="<?php echo base_url(); ?>index.php/auth/login/" id="login-form" method="post">
  <div class="form-group has-feedback">
    <input type="text" class="form-control" placeholder="Username" id="username" name="username" required>
    <span class="glyphicon glyphicon-user form-control-feedback"></span>
  </div>
  <div class="form-group has-feedback">
    <input type="password" class="form-control" placeholder="Password" id="password" name="password" required>
    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
  </div>
  <div class="form-group">
    <p id="captImg" class="captcha-img"><?php echo $captchaImg; ?></p>
    <input type="text" class="form-control" name="captcha" placeholder="Captcha" style="margin-bottom: 5px"/>
    <a href="#" class="reload-captcha refreshCaptcha btn btn-info btn-sm" ><i class="glyphicon glyphicon-refresh"></i></a>
  </div>
  <div class="row">
    <!-- /.col -->
    <div class="col-xs-8">
          <label><a href="#"><u>Forgot your password?</u></a></label>
    </div>
    <div class="col-xs-4 pull-right">
      <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
    </div>
    <!-- /.col -->
  </div>
</form>

1 个答案:

答案 0 :(得分:0)

发生了什么样的问题? 试试:

$('.captcha-img').attr("src", data);

取代:

$('.captcha-img').replaceWith(data);

并添加

dataType: "text",  
  cache:false,

如下:

$.ajax({
             url:base_url+'index.php/auth/refresh_login',
             dataType: "text",  
             cache:false,
             success:function(data){
                $('.captcha-img').attr("src", data);
             }
         });