Home>Article>Backend Development> Detailed explanation of the steps to submit a form in the form of an array using the yii framework form model

Detailed explanation of the steps to submit a form in the form of an array using the yii framework form model

php中世界最好的语言
php中世界最好的语言 Original
2018-05-21 10:00:36 1126browse

This time I will bring you a detailed explanation of the steps for submitting a form in the form of an array using the yii framework form model. What are theprecautionsfor the yii framework form model to submit a form in the form of an array? The following is a practical case. Let’s take a look. one time.

According to the description in Yii documentation, the general process of Yii processing forms is:
Create themodel class corresponding to the form, and set the fieldsValidation rules
Create the action corresponding to the form submission and process the submitted content
Create in the viewForm
In a small project just now, I wanted to use ajax to submit the form information and verify and save it, but I didn’t want to Use a hidden iframe for non-refresh submission, and the verification method of the model class can be used in the action, so I thought of using a form array to submit.
Example, form code:

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

Array ( [0] => a [1] => b [2] => c )

Similarly, if you use the following form to submit:

$_POST['arr'] Array ( [3] => a [6] => b [8] => c )

Of course you can also submittwo-dimensional array:

$_POST['arr'] 为:Array( [0] => Array ( [name1] => a ) [1] => Array ( [name2] => b ) [2] => Array ( [name3] => c ))

There is a problem here, If you do not set the key of the first sub-array, each value will be added to arr sequentially when generating the array. If you want to save the information in an array, just add a key value, as follows:

$_POST['arr'] 为:Array( [a] => Array ( [name1] => a1 [value1] => a2 ) [b] => Array ( [name2] => b1 [value2] => b2 ))

Use ajax to submit the form and use yii formModel verificationExample, first is the model class part, only the simplest verification method:

''), ); } }

Model class When setting the parameter verification method, you need to set rules for each public parameter. If there are parameters without set rules, after assigning values to the model using the form value in $_POST, the parameter values without set rules will be empty# Get the parameters submitted by the form in ##action and verify them:

$model = new LandingForm; $model->attributes = $_POST['form']; if($model->validate()){ $info = $model->attributes; ... }

Finally, the code for the front-end submission form part, using jquery:


var info = new Object(); info = { 'form[landing_title]': landing_title, 'form[landing_content]': landing_content, 'form[landing_position]': landing_position, }; var url = "..."; $.post(url, info, function(rst){ ... });

I believe I have read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps to create scheduled tasks through the yii framework through console commands

PHP accelerator eAccelerator configuration and usage steps Detailed explanation

The above is the detailed content of Detailed explanation of the steps to submit a form in the form of an array using the yii framework form model. For more information, please follow other related articles on the PHP Chinese website!

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