Home  >  Q&A  >  body text

jquery - thinkphp3.2.3's verification code automatically refreshes

How to write automatic refresh of Ajax verification code error in thinkphp?
Write it here in else of this return function

Attach the following html


How to trigger this click time again where else?

迷茫迷茫2713 days ago507

reply all(6)I'll reply

  • 黄舟

    黄舟2017-05-16 13:18:38

    You can write the verification code yourself, or you can use a third-party library. There are many third-party libraries now. For the refresh mechanism, it is recommended to use the API method. Just write a Javascript to give a random number to distinguish.
    Example:
    Here I am using the third-party verification code library gregwar/captcha

    <input type="text" name="captcha" class="form-control" style="width: 300px;">
              <a onclick="javascript:re_captcha();" ><img src="{{ URL('code/captcha') }}"  alt="验证码" title="刷新图片" width="100" height="40" id="code" border="0"></a>
    
    <script>  
      function re_captcha() {
        $url = "{{ URL('kit/captcha') }}";
            $url = $url + "/" + Math.random();
            document.getElementById('code').src=$url;
      }
    </script>

    If you mistakenly refresh automatically, call the re_captcha method again.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:18:38

    The principle of refreshing the verification code is actually very simple, just add a suffix?t=123123

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:18:38

    else{
       $('#img').src = 'url?rand='+Math.random(); 
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 13:18:38

    1. Refresh in the else part of the callback. The method is given above.

    2. If you have done the first point and it has not been refreshed, then I guess there is something wrong with your judgment:
    if(data.info==1)
    Here, should it be: if(data.status== 1) ????

    3.$("#2")What is it? The ID cannot start with a number.

    reply
    0
  • PHPz

    PHPz2017-05-16 13:18:38

    My personal habits are probably like this

    1. Save a src on the verification code image and save the original address of the verification code (to prevent the image address from getting longer)

    2. Bind click switching event

    3. When the verification code is wrong, the verification code click event is released through trigger to switch the verification code

    //代码手写 难免有误
    $(function(){
        var verifyImg = $("#verify_img");
        verifyImg.click(function(){
            $(this).attr("src",$(this).data('src') + '?v=' + Math.random());
        }).data('src', verifyImg.attr('src'));
        
        $("#fm-xxx").submit(function(){
            var fm = $(this);
            $.post(fm.attr('action'), fm.serialize(), function(data){
                if(data.code == 'verify-code') {
                    verifyImg.trigger("click");
                } else {
                    //...
                }
            });
            return false;
        });
    });

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:18:38

    Add below

    $("#2").html(验证码错误);
    
    $("#verify_img").attr("src",<?php echo U('Index/yzm');?>);

    reply
    0
  • Cancelreply