laravel form表单提交的控制器重定向无效_【20201212】做个用户管理系统(25)——修改个人资料(二)表单数据载入...

1. 介绍

1.1 介绍

福哥今天带着大家完成修改个人资料表单的后一部分,实现表单的数据载入功能。要实现表单数据的载入首先得开发一个表单数据的接口,这个表单数据接口需要对应一个单独的实体对象。根据资源路径命名规范,路径里只能出现资源名称,不能出现事件行为。提交表单和数据载入都属于用户的个人资料数据,不能建立两个个人资料接口,怎么办?

我们可以使用资源路径地址实现表单数据的读取功能,然后使用资源事件地址实现表单数据的提交功能。

资源路径地址和资源事件地址的区别就是后者在前者的后面增加了一个以下划线开头的事件参数,有了这个参数就表单该地址是用来处理事件的。

2. 接口控制器

2.1 doSave

protected function doSave(){    $req = $this->tfphp->getRequest();    $post = $req->post;    $user = new user($this->tfphp);    $nick = $post->get("nick");    $gender = $post->get("gender");    $iGender = Dictionary::getArrId(Dictionary::$genderArr, $gender);    $year = $post->get("year");    $month = $post->get("month");    $edu = $post->get("edu");    $iEdu = Dictionary::getArrId(Dictionary::$eduArr, $edu);    $note = $post->get("note");    try{        // request test        if($nick == "" || $gender == "" || $year == "" || $month == ""){            return $this->tfphp->getResponse()->responseJSON_CM(200, 1001031, "错误请求");        }        // create user        $ret = $user->setProfile($this->permission->getLoginStatus()->userID,            $nick,            $iGender,            $year."-".$month."-1",            $iEdu,            $note);        switch ($ret){            case 1:                return $this->tfphp->getResponse()->responseJSON_CM(200, 1001032, "用户名不存在");                break;            case 2:                return $this->tfphp->getResponse()->responseJSON_CM(200, 1001033, "保存个人资料失败");                break;        }    }    catch(\TypeError $e){        return $this->tfphp->getResponse()->responseJSON_CM(200, 1001031, "错误请求");    }    // output    return $this->tfphp->getResponse()->responseJSON_CM(200, 0, "OK");}

2.2 doLoad

private function doLoad(){    $user = new user($this->tfphp);    $userProfile = new userProfile();    $userProfile = $user->getProfile($this->permission->getLoginStatus()->userID);    $userProfile->iGender = Dictionary::getArrId(Dictionary::$genderArr, $userProfile->gender);    $userProfile->iTopEdu = Dictionary::getArrId(Dictionary::$eduArr, $userProfile->topEdu);    return $this->tfphp->getResponse()->responseJSON(200, $userProfile->toArray());}

2.3 user_process

protected function user_process(){    switch ($this->tfphp->getRequest()->server->get("ACTION_URI")){        case "save":            $this->doSave();            break;        default:            $this->doLoad();            break;    }}

3. 视图模板

3.1 JS代码

$(function(){    // form    $('form').form({        url: "server->BASE_URI %>api/member/profile/_save",        method: "post",        validations: [            {type:"empty", name:"nick", msg:"请填写昵称"},            {type:"empty", name:"gender", msg:"请选择性别"},            {type:"empty", name:"year", msg:"请选择生日"},            {type:"empty", name:"month", msg:"请选择生日"},        ],        dataUrl: "server->BASE_URI %>api/member/profile",        dataMethod: "post",        dataMapRules: [            {name:"nick", dataKey:"nickName"},            {name:"gender", dataKey:"gender"},            {name:"year", dataKey:"year"},            {name:"month", dataKey:"month"},            {name:"edu", dataKey:"topEdu"},            {name:"note", dataKey:"descript"}        ],        onSuccess: function (d) {            if(d.errcode == 0){                document.location = 'server->BASE_URI %>member/profile.htm';            }            else{                $('form').tips({                    text:d.errmsg                });            }        },        onError: function (d) {            $('form').tips({                text:"服务器响应错误"            });        },        onValidationError: function (form, name, msg) {            $('form').tips({                text:msg            });            $('form').find('[name="'+ name +'"]').focus();        },        onDataMap: function (form, name, data) {        }    });});

4. 讲解

4.1 框架控制器

4.1.1 doSave

这个方法就是之前的user_process,因为我们需要在api/member/profile接口里完成表单提交和数据加载两个功能,所以需要把表单提交单独放一个方法里面。

4.1.2 doLoad

这个方法是用来加载修改个人资料表单的数据的。

4.1.3 user_process

我们使用TFPHP自带的系统参数ACTION_URI来判断是否有事件参数,如果有则进行分流处理。

e3a53dd93a3d99815b2f252d33980232.png

5. 总结

今天福哥带着童鞋们完成了修改个人资料的表单数据载入的功能,完成了这个功能之后,修改个人资料表单也就算完成了。

下一课,福哥将开始带着童鞋们制作设置头像表单了哦~~


【20201211】做个用户管理系统(24)——修改个人资料(一)提交表单

【20201210】做个用户管理系统(23)——授权模式的设计和使用


免费看文章,自己学技术


每一篇文章都是福哥一个字一个字地敲出来的,都是福哥原创的。

每一篇文章都是经过了福哥的反复验证的,都是可以正常使用的。


1dfd83527b3790c94c4e98446af26d8f.png

4fe1553be7dfc1dfa543197b84badd40.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值