Get the App
SLTechnology News&Howtos  ›  Development  › 

Yii Framework form Model usage and how to submit form data in an Array form tutorial

Shulou Source: shulou.com Published: 2022-06-03 08:07:47 10月24日 Update

This article mainly introduces the "tutorial on the use of yii Framework form Model and the method of submitting form data in Array form". In daily operation, it is believed that many people have doubts about the use of yii Framework form model and the method tutorial of submitting form data in array form. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer your doubts about how to use the form model in yii framework and how to submit form data in array form. Next, please follow the editor to study!

As described in the Yii document, the general process for Yii to process a form is:

Create the model class corresponding to the form and set the field validation rules

Create the action corresponding to the form submission and process the submitted content

Create a form form in the view

In a small project just now, you want to use ajax to submit form information and verify and save it, but you don't want to use hidden iframe to do no refresh submission, and action can use model class verification methods, so you want to use form array submission, for example:

Form Code:

The copy code is as follows:

After submission, you can directly use $_ POST ['arr'] to obtain the submitted data. $_ POST [' arr'] is:

The copy code is as follows:

Array

(

[0] = > a

[1] = > b

[2] = > c

)

Similarly, if you submit using the following form:

The copy code is as follows:

$_ POST ['arr'] is:

Array

(

[3] = > a

[6] = > b

[8] = > c

)

Of course, you can also submit a two-dimensional array:

The copy code is as follows:

$_ POST ['arr'] is:

Array

(

[0] = > Array

(

[name1] = > a

)

[1] = > Array

(

[name2] = > b

)

[2] = > Array

(

[name3] = > c

)

)

Here is a problem. If you do not set the key of the first subarray, each value will be added in the arr when the array is generated. If you want to save the information in an array, add a key value, as follows:

The copy code is as follows:

$_ POST ['arr'] is:

Array

(

[a] = > Array

(

[name1] = > A1

[value1] = > a2

)

[B] = > Array

(

[name2] = > b1

[value2] = > b2

)

)

Here is an example of submitting a form with ajax and validating it with a yii form model, starting with the model class section, where there is only the simplest verification method:

The copy code is as follows:

Tags: Forms code models arrays methods data parameters forms tutorials frameworks rules learning validation information more parts problems processing help practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL macOS Shulou Technology Linux