Home > Backend Development > PHP Tutorial > Example of using yii framework form model and submitting form data in array form_PHP tutorial

Example of using yii framework form model and submitting form data in array form_PHP tutorial

WBOY
Release: 2016-07-13 10:31:33
Original
825 people have browsed it

According to the description in the Yii documentation, the general process of Yii processing forms is:

Create the model class corresponding to the form, set the field validation rules
Create the action corresponding to the form submission, and process the submitted content
Create the form form in the view
In a small project just now, I want to use Ajax submits the form information and verifies and saves it. I don’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. I thought of using the form array submission method. For example:

form code:

Copy code The code is as follows:







Submit Then you can directly use $_POST['arr'] to get the submitted data. $_POST['arr'] is:
Copy the code The code is as follows:

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

Similarly, if you use the following form to submit:
Copy the codeThe code is as follows:






$_POST['arr'] is:

Array
(
[3] => a
[6] => b
[8] => c
)


Of course, you can also submit a two-dimensional array:
Copy the code The code is as follows:


     
    

$_POST['arr'] is:


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:

Copy code The code is as follows:




< ;input type='text' name='arr[b][name2]' value='b1'>
 

$_POST['arr'] is:

Array
(
[a] => Array
(
[name1] => a1
[value1] => a2
)
[ b] => Array
                                                                                                                                                                                            🎜>
Posted below is an example of using ajax to submit a form and verifying it using the yii form model. The first is the model class part, which only has the simplest verification method:



Copy code
The code is as follows:

class LandingForm extends CFormModel{ public $landing_title; public $landing_content; public $landing_position;

public function rules()
{
return array(
array('landing_title, landing_content', 'required'),
array('landing_position', 'default', 'value' =>''),
);
}

}




I found something interesting. When setting the parameter verification method, the model class needs to set rules for each public parameter. If there are parameters for which rules are not set, use the form value in $_POST to assign values ​​to the model. After that, the parameter value of the unset rule will be empty

Get the parameters submitted by the form in action and verify:


Copy code

The code is as follows:

$model = new LandingForm;

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


The last is the code for the front-end submission form, using jquery:




Copy code
The code is as follows:

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){
...
});


http://www.bkjia.com/PHPjc/763016.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/763016.html

According to the description in the Yii document, the general process of Yii processing the form is: Create the model class corresponding to the form, and set Field validation rules create actions corresponding to form submission and process submitted content...
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