html 文件
<form class="form-signin loginform" role="form">
<input type="text" id="logName" name="name" class="form-control login_user" placeholder="用户名/手机号" required autofocus />
<input type="password" id="logPass" name="pass" class="form-control login_pass" placeholder="密码" required />
<input type="hidden" id="url" value="{:U('User/log')}" />
<input type="hidden" id="indexUrl" value="{:U('Index/index')}" />
<button class="btn btn-lg btn-warning btn-block" id="loginForm">登录</button>
<label class="checkbox">
<!-- <input type="checkbox" value="remember-me"> -->
<a href="#" class="pull-right" style="margin-left:-20px;">忘记密码?</a>
</label>
<span>使用第三方快捷登录</span>
<div class="otherLogin">
<ul>
<li><span class="qqLogin" title="QQ"></span></li>
<li><span class="wxLogin" title="微信"></span></li>
</ul>
</div>
</form>
js文件
$(document).ready(function(){
$('#loginForm').click(function (){
var name = $('#logName').val();
var pass = $('#logPass').val();
var datas={
name:name,
pass:pass
};
var url = $('#url').val();
var indexUrl = $('#indexUrl').val();
$.ajax({
url:url, //后台处理程序
type:'post', //数据传送方式
dataType:'json', //接受数据格式
data:datas, //要传送的数据
success:function(reslut){
if(reslut == 'success'){
window.location.href=indexUrl;
}else if(reslut == 'nameError'){
alert("用户名不存在");
}else if(reslut == 'passError'){
alert("密码不正确");
}
},
error:function(){
}
});
});
controller 文件
public function log(){
$name = $_POST['name'];
$pass = $_POST['pass'];
$m = M("user");
$val['name'] = $name;
//$val['password'] = $pass;
$msg = $m->where($val)->find();
if($msg == true){
if(decrypt($msg['password']) == $pass){
cookie("username",$name);
echo json_encode('success');
}else{
echo json_encode('passError');
}
}else{
echo json_encode('nameError');
}
}