douqin2108 2017-08-11 01:03
浏览 107
已采纳

刷新验证码图像jquery codeigniter

I have problem with my refresh captcha button, it just work once when I click the button after I reload the page. I dont know if i wrong or if I missed something else.

I try to refresh the captcha with jquery because more efficiently.

This is piece of my code :

controller

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);
             }
         });            
      });

view

<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条回答 默认 最新

  • douliedu335997 2017-08-11 06:33
    关注

    what kind of problem is occurring ??? try :

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

    in place of :

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

    and add

    dataType: "text",  
      cache:false,
    

    as follow :

    $.ajax({
                 url:base_url+'index.php/auth/refresh_login',
                 dataType: "text",  
                 cache:false,
                 success:function(data){
                    $('.captcha-img').attr("src", data);
                 }
             }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择