Home >Web Front-end >JS Tutorial >Web production verification code function example code
Implementation effect:
Implementation principle:
Generate verification code provided by the background The Verification codestring Save it in the Session of the backend, and wait for the frontend to request the business interface again and compare it with the verification code string in the session.
Implementation ideas:
1. First, the back-end provides an interface that can produce verification code images 2. The front-end sets the src attribute in img , an interface for requesting verification code generation. 3. Set a click event for img. Every time you click img, the src value will be changed and the src request will be re-requested 4. When doing page interface operations, check whether the entered verification code is correct.Implementation code:
Front-end:
html:<p class="centent-top" style=""> <p class="centent-left"><span>*</span>验证码:</p> <input type="text" class="verification-code-input"> <p class="verification-code"><img id="yzm" src="/SchoolRoll/accuser/code/check"></p> <p class="change"><span>看不清?</span><span style="color:#37CAF2;cursor: pointer;" id="changeImgCode">换一张</span></p> </p>js:
var yzm =document.getElementById("yzm"); var changeImgCode =document.getElementById("changeImgCode"); yzm.onclick=function () { changPin(); } changeImgCode.onclick=function () { changPin(); } // 换验证码 function changPin() { $("#yzm").attr("src", "/SchoolRoll/accuser/code/check?time=" + Math.random()); }
The above is the detailed content of Web production verification code function example code. For more information, please follow other related articles on the PHP Chinese website!