Yii form model usage and submitting form data in array format _ PHP Tutorial

Source: Internet
Author: User
Use the Yii form model and submit form data in array form. According to the description in the Yii document, the general process of Yii processing forms is: in a small project just now, Yii wants to submit form information using ajax and verify and save the information, you do not want to use the hidden iframe to do the following:

In a small project just now, I want to use ajax to submit form information and verify and save the information, and do not want to use hidden iframe for refreshing new submissions. In addition, the action can be used to validate model classes, let's think of the form array submission method. for example:

Form code:


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

0] =>1] =>2] =>

Similarly, if you use the following form to submit:


$ _ POST ['arr:

3] =>6] =>8] =>

Of course, two-dimensional arrays can also be submitted:


$ _ POST ['arr:

0] => =>1] => =>2] => =>

If the key of the first sub-array is not set, each value is added to arr when an array is generated. if you want to save the information in an array, add a key value as follows:


$ _ POST ['arr:

=> =>=>=> =>=>

The following is an example of submitting a form using ajax and verifying the form using the yii form model. The first part is the model class. there is only the simplest verification method:

 ''),

It is interesting to find that when the model class sets the parameter verification method, it needs to set rules for each public parameter. if there are parameters with no rules set, after the form value in $ _ POST is assigned a value to the model, the parameter value without the rule is null.

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

$model->attributes = ['form'(-> = ->......

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

 info = = { 'form[landing_title]''form[landing_content]''form[landing_position]' url = "..."

In a small project just now, users want to use ajax to submit form information and verify and save the information, and do not want to use hidden iframe to do this...

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.