According to the description in the Yii document, the general process of Yii in form processing is: create the model class corresponding to the form, set the field verification rule to create the corresponding action for form submission, process submitted content and create a form in the view. in a small project just now, if you want to use ajax to submit the form information and verify and save it, you do not want to use hidden iframe for refreshing the new submission, in addition, the validation method of model classes can be used in the action, so you can use the form array submission method. 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, two-dimensional arrays can also be submitted:
$ _ POST ['arr']: Array ([0] => Array ([name1] => a) [1] => Array ([name2] => B) [2] => Array ([name3] => c). if the key of the first sub-Array is not set, each value is added in the arr sequence when an array is generated. to save the information in an array, add a key value as follows:
-
$ _ POST ['arr']: Array ([a] => Array ([name1] => a1 [value1] => a2)=> Array ([name2] => b1 [value2] => b2 )) The following is an example of submitting a form using ajax and verifying the form using the yii form model:
- Class LandingForm extends CFormModel
- {
- Public $ landing_title;
- Public $ landing_content;
- Public $ landing_position;
- Public function rules ()
- {
- Return array (
- Array ('logging _ title, landing_content ', 'required '),
- Array ('logging _ position', 'default', 'value' => ''),
- );
- }
- }
-
When setting the parameter verification method for a model class, you need to set rules for each public parameter. if there is a parameter with no rules set, after you assign a value to the model using the form value in $ _ POST, if no rule is set, the parameter value is null and the parameter submitted by the form is obtained and verified:
- $ Model = new LandingForm;
- $ Model-> attributes = $ _ POST ['form'];
- If ($ model-> validate ()){
- $ Info = $ model-> attributes;
- ...
- }
-
Finally, the code for the front-end form submission, 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 ){
- ...
- });
|