HTML code
<offcondition= "!isset ($_session[' account ')"> <Divclass= "Load lf"> <P>Register Login</P> <Divclass= "UserId">Account Number:<inputtype= "text"placeholder= "Please enter account number"value=""name= "username"ID= "username"Required> </Div> <Divclass= "Userpwd">Password:<inputtype= "Password"placeholder= "Please enter password"value=""name= "pwd"ID= "pwd"Required> </Div> <Divclass= "User-choose"> <label> <inputtype= "checkbox"name= "UserId"> <Divclass= "Remember">Remember user name</Div> </label> <ahref="#"class= "Forget">Forgot password</a> </Div> <Divclass= "Bottom-choose"> <Buttonclass= "Submit"ID= "Submit"type= "Submit"></Button> <ahref= "{{: U (' Register/index ')}}"class= "Register"></a> </Div> </Div> <Else/> <Divclass= "Load lf">{{$Think. Session.account}}<ahref= "{{: U (' Index/loginout ')}}">Exit</a> </Div>
JS Code
<Script>$ (document). Ready (function(){ $("#submit"). Click (function(){ varusername= $("#username"). Val (); varpwd= $("#pwd"). Val (); $.post ("{{: U (' Index/login ')}}", {username:username,pwd:pwd},function($data) {if($data. Status== 1) {Location.href=$data. url; }Else{alert ($data. info); $('#username'). Reset (); $('#pwd'). Reset (); } }); }); });</Script>
PHP code
Public functionLogin () {$username=$_post[' username ']; $pwd=$_post[' pwd ']; if($username= = ' | |$pwd= = "'){ $this->ajaxreturn (Array(' Status ' =>0, ' info ' = ' account password cannot be empty ', ' url ' = ' = ')); } $User= D ("User"); $result=$User->getfields ("account=").$username. "' and password= '".MD5($pwd)."‘"); if(Empty($result)){ $this->ajaxreturn (Array(' Status ' =>0, ' info ' = ' login failed ', ' url ' = ' = ')); }Else{session (' Account ',$result[' Account ']); $url=u (' Index/index '); $this->ajaxreturn (Array(' Status ' =>1, ' info ' = ' login succeeded ', ' url ' = =$url)); } } Public functionLoginout () {session (' Account ',NULL); $this->redirect (' Index/index '); }
Model Code
Public function getfields ($where) { return$this->where ($where ),find (); }
thinkphp3.2 User Login Ajax Commit Validation