//编辑用户资料
public function admin_edit()
{
$id=input('id');
$admins=db('admin')->find($id);
if(request()->isPost()){
$data=[
'id'=>input('id'),
'username' => input('username'),
'password' => md5(input('password')),
'uname' => input('uname'),
'usertype' => input('usertype'),
'email' => input('email'),
'signature' => input('signature'),
'introductions' => input('introductions'),
'logintime' => time(),
'loginip' =>request()->ip(),
'phone' => input('phone'),
'sex' => input('sex')
];
if(input('password')){
$data['password']=md5(input('password'));
} else {
$data['password']=$admins['password'];
}
$validate = validate('Admin');
if(!$validate->scene('edit')->check($data)){
$this->error($validate->getError());
}
$save = db('admin')->where('id',$id)->update($data);
if($save){
return json($data);
//return;
}else{
$data2 = 404;
return $this->ajaxReturn($data2,'修改失败',0);
}
return;
}
$this->assign('admins',$admins);
return $this->fetch('admin-edit');
}
Js部分:
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
$(function(){
$('.skin-minimal input').iCheck({
checkboxClass: 'icheckbox-blue',
radioClass: 'iradio-blue',
increaseArea: '20%'
});
$("#form-admin-add").validate({
//验证规则
rules:{
username:{
required:true,
minlength:4,
maxlength:16
},
uname:{
required:true,
minlength:4,
maxlength:16
},
password:{
required:true,
},
password2:{
required:true,
equalTo:'#password'
},
usertype:{
required:true,
number:true
},
email:{
required:true,
email:true
},
signature:{
required:true,
minlength:4,
maxlength:100
},
},
//验证设置
onkeyup:true, //在 keyup 时不验证
focusCleanup:true,
success:"valid",
submitHandler:function(form){
$(form).ajaxSubmit({
type: 'post',
dataType: "json",
url: "{:url('admin/admin_edit')}",
success: function(data){
//alert(data);
layer.msg('修改成功!', {icon:1,time:1000}, function(){
var index = parent.layer.getFrameIndex(window.name);
parent.location.reload(); //刷新父页面
parent.layer.close(index);
});
},
error: function(XmlHttpRequest, textStatus, errorThrown){
layer.msg('error!',{icon:1,time:1000});
}
});
}
});
});
</script>