How does ActiveForm submit asynchronously?
1. First configure the following attributes in the ActiveForm of the views layer
), //此处为请求地址 Url用法查看手册 'enableAjaxValidation' => true, 'validationUrl' => Url::to(['post/validate']), //数据异步校验 ]); 2.配置好表单属性之后,在控制器中添加对应的方法 validateUrl对应的方法 //该方法是异步校验字段,输入框失去焦点之后自动会自动请求改地址 public function actionValidate(){ $model = new PostModel(); if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } } action对应的方法 //该方法是数据保存方法 public function actionSave() { $model = new PostCatModel(); if ($model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; return ['status' => $model->save()]; } }
In addition, you also need to write a submitted js
[code=php;toolbar:false">//此处点击按钮提交数据的jquery $('.btn').click(function () { $.ajax({ url: "/post/save.html", type: "POST", dataType: "json", data: $('form').serialize(), success: function(Data) { if(Data.status) alert('保存成功'); else alert('保存失败') }, error: function() { alert('网络错误!'); } }); return false; });
Note: the corresponding in actionSave The namespace must be added, otherwise an error will be reported
For example, Response and ActiveForm
use yiiwebResponse; use yiiootstrapActiveForm;[/code]
The above is the asynchronous submission method corresponding to ActiveForm
Above It is the ajax submission method content of Yii2.0 ActiveForm component. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
