search
Homephp教程PHP开发Ajax submission method of Yii2.0 ActiveForm component

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)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)