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='' method='post' name='form_test'> <input type='text' name='arr[]' value='1'> <input type='text' name='arr[]' value='2'> <input type='text' name='arr[]' value='3'> </form>
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:
<form action='' method='post' name='form_test'> <input type='text' name='arr[3]' value='a'> <input type='text' name='arr[6]' value='b'> <input type='text' name='arr[8]' value='c'> </form> $_POST['arr'] Array ( [3] => a [6] => b [8] => c )
Of course you can also submit two-dimensional array:
<form action='http://127.0.0.1/zhaobolu/test.php' method='post' name='form_test'> <input type='text' name='arr[][name1]' value='a'> <input type='text' name='arr[][name2]' value='b'> <input type='text' name='arr[][name3]' value='c'></form> $_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:
<form action='http://127.0.0.1/zhaobolu/test.php' method='post' name='form_test'> <input type='text' name='arr[a][name1]' value='a1'> <input type='text' name='arr[a][value1]' value='a2'> <input type='text' name='arr[b][name2]' value='b1'> <input type='text' name='arr[b][value2]' value='b2'></form> $_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:
<?php 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'=>''), ); } }
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; ... }
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){ ... });
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!