thinkphp5 使用H-ui+ ajax 的一些记录

浏览:3874 发布日期:2017/06/15 分类:技术分享 关键字: ajax thinkphp5
作为一个新手,学习tp5,使用h-ui做后台,趟过很多坑,关于ajax是最严重的一点。为了解决这个ajax,搞懂ajax基本用法,看了《javascript DOM编程艺术(第2版)》、《锋利的jQuery(第2版)》,记录一下,以备查询。

控制器脚本:    //编辑用户资料
    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>  
最佳答案
评论( 相关
后面还有条评论,点击查看>>