Yii Framework Form model use and submit form data as an array example _php instance

Source: Internet
Author: User
Tags array example yii

As described in the Yii documentation, the general process of yii processing a form is:

Create the model class for the form and set the field validation rules
Create a form submission corresponding action, process the content submitted
Create the form form in a view
In just a small project, want to use AJAX to submit form information and verify the save, do not want to use hidden iframe to do without the refresh submission, and the action can use the model class validation method, think of the use of the form singular group submission method, for example:

Form code:

Copy Code code as follows:

<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>

The submitted data can be obtained directly using $_post[' arr ' after submission, $_post[' arr ' is:
Copy Code code as follows:

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

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

<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 '] is:

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


You can certainly submit two-dimensional arrays:
Copy Code code as follows:

<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 '] is:


Array
(
[0] => Array
(
[Name1] => A
)

[1] => Array
(
[Name2] => b
)

[2] => Array
(
[Name3] => C
)
)

Here's the problem, if you don't set the key for the first sub array, add the order of each value to the ARR when you generate the array, and if you want to save the information in an array, add a key value, as follows:

Copy Code code 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 '] is:

Array
(
[A] => Array
(
[Name1] => A1
[Value1] => A2
)
[b] => Array
(
[Name2] => B1
[value2] => B2
)
)


Here's an example of submitting a form with Ajax and validating it with a yii form model, first with the Model class section, with the simplest checksum method:

Copy Code code as follows:

<?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 ' => '),
);
}
}

The interesting thing is that the model class needs to set the rules for each public parameter when setting the method of the parameter checksum, and if there are parameters that do not set the rule, the parameter value for the rule is not set to NULL when the value of the form in $_post is assigned to the model.

Action to get the form submit parameters and verify:

Copy Code code as follows:

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

Finally, the front end submits the form part of the code, using the jquery:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.