Thinkphp in the verification code can be called directly, very convenient, we look at the Think folder has a file named verify.class.php
First we have a template, in the View folder to write a file named xx.html, there is nothing to write, then we have to show in this file a verification code to
function xx () { $this-Show ();} function Yzm () { $yzm=new \think\verify (); $yzm-entry ();}
Want the verification code to show, then we need to adjust the Yzm method, in the xx.html is expressed as
< Div > < src= "__controller__/yzm" id= "img1"/></Div >
So that the verification code can be transferred out, after the verification code is called to use it, then there will be an input form in the xx.html will have a validation button
< div > < input =" text " ID = "va" /> < input type = "button" ID =" btn " /> </ div >
The JS Part I wrote below is jquery, so I have to introduce the jquery package.
<script type= "Text/javascript" > $("#btn"). Click (function(){ varVA = $ ("#va"). Val (); $.ajax ({URL:"__controller__/yz", Data:{va:va}, type:"POST", DataType:"TEXT", Success:function(data) {if(data) {alert ("Validation succeeded"); } Else{alert ("Validation Failed"); } } }); })</script>
Now we're going to write the Yz method.
function yz () { $yzm=new \think\verify (); $va=$_post["va"]; $aa=$yzm->check ($va); $this->ajaxreturn ($aa, "eval");}
After this is finished, we will find a problem is that when you click on the image will not automatically change, then we have to add a click on the picture, each click to let the path again go again
$ ("#img1"). Click (function() {/// if only run $ (this) on other browsers except IE browser . attr ("src", "__controller__/yzm"); // if we want to increase their compatibility, then we're going to take a random number. $a=math.random () *10; $ (this). attr ("src", "__CONTROLLER__/YZM/AA" +a);})
thinkphp Verification Code