Home > Backend Development > PHP Tutorial > 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中世界最好的语言
Release: 2023-03-27 06:48:02
Original
1222 people have browsed it

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 the precautions for 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 the model class corresponding to the form, and set the fields Validation 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:

<form action=&#39;&#39; method=&#39;post&#39; name=&#39;form_test&#39;>
    <input type=&#39;text&#39; name=&#39;arr[]&#39; value=&#39;1&#39;>
    <input type=&#39;text&#39; name=&#39;arr[]&#39; value=&#39;2&#39;>
    <input type=&#39;text&#39; name=&#39;arr[]&#39; value=&#39;3&#39;>
</form>
Copy after login

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

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

Similarly, if you use the following form to submit:

<form action=&#39;&#39; method=&#39;post&#39; name=&#39;form_test&#39;>
    <input type=&#39;text&#39; name=&#39;arr[3]&#39; value=&#39;a&#39;>
    <input type=&#39;text&#39; name=&#39;arr[6]&#39; value=&#39;b&#39;>
    <input type=&#39;text&#39; name=&#39;arr[8]&#39; value=&#39;c&#39;>
</form>
$_POST[&#39;arr&#39;]
Array
(
    [3] => a
    [6] => b
    [8] => c
)
Copy after login

Of course you can also submit two-dimensional array:

<form action=&#39;http://127.0.0.1/zhaobolu/test.php&#39; method=&#39;post&#39; name=&#39;form_test&#39;>    <input type=&#39;text&#39; name=&#39;arr[][name1]&#39; value=&#39;a&#39;>    <input type=&#39;text&#39; name=&#39;arr[][name2]&#39; value=&#39;b&#39;>    <input type=&#39;text&#39; name=&#39;arr[][name3]&#39; value=&#39;c&#39;></form>
$_POST[&#39;arr&#39;] 为:Array(    [0] => Array        (            [name1] => a        )    [1] => Array        (            [name2] => b        )    [2] => Array        (            [name3] => c        ))
Copy after login

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:

<form action=&#39;http://127.0.0.1/zhaobolu/test.php&#39; method=&#39;post&#39; name=&#39;form_test&#39;>    <input type=&#39;text&#39; name=&#39;arr[a][name1]&#39; value=&#39;a1&#39;>    <input type=&#39;text&#39; name=&#39;arr[a][value1]&#39; value=&#39;a2&#39;>    <input type=&#39;text&#39; name=&#39;arr[b][name2]&#39; value=&#39;b1&#39;>    <input type=&#39;text&#39; name=&#39;arr[b][value2]&#39; value=&#39;b2&#39;></form> 
$_POST[&#39;arr&#39;] 为:Array(    [a] => Array        (            [name1] => a1            [value1] => a2        )    [b] => Array        (            [name2] => b1            [value2] => b2        ))
Copy after login

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

<?php
class LandingForm extends CFormModel
{
    public $landing_title;
    public $landing_content;
    public $landing_position;
    public function rules()
    {
        return array(
            array(&#39;landing_title, landing_content&#39;, &#39;required&#39;),
            array(&#39;landing_position&#39;, &#39;default&#39;, &#39;value&#39;=>&#39;&#39;),
        );
    }
}
Copy after login

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[&#39;form&#39;];
if($model->validate()){
    $info = $model->attributes;
    ... 
}
Copy after login

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


var info = new Object();
info = { &#39;form[landing_title]&#39;: landing_title,
        &#39;form[landing_content]&#39;: landing_content,
        &#39;form[landing_position]&#39;: landing_position,
        };
var url = "...";
$.post(url, info, function(rst){
    ... 
});
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template