PHP Development Framework YII Framework Tutorial (7) Creating a form using cHTML

Source: Internet
Author: User
Tags yii
When creating a view of an MVC application, you can directly construct the page view directly using the HTML language, and Yii also provides several helper classes to simplify the view writing. For example, to create a text input field, we can call Chtml::textfield (); To create a drop-down list, call CHtml::d ropdownlist (). Some of the view in the previous hangman example is using cHTML to create the view.

Info: You might want to know the benefits of using helper classes if they need the same amount of code as the amount of code that writes pure HTML directly. The answer is that the help class can provide more functionality than the HTML code. For example, the following code generates a text input field that can touch the post-commit action when the user modifies its value.

Chtml::textfield ($name, $value, Array (' submit ' = ') ');

Otherwise you'll need to write a lot of JavaScript. 。

Recall the definition of page play in Hangman:

<p>this is the game of hangman.     You must guess a word, a-letter at a time.     If you do too many mistakes, you lose the game!</p> <?php     echo chtml::beginform ();?>     <?php Echo Chtml::radiobuttonlist (' level ', null, $levels);?>     <br/>     <?php echo Chtml::submitbutton (' play! '); ?>     <?php if ($error):?>     <span style= "color:red" >you must choose a difficulty level!</span >     <?php endif; ><?php echo Chtml::endform ();?>

Using the CHtml Construction page always starts with Chtml::beginform () and ends with Chtml::endform (). In fact, these two methods generate a start and end tag for the from tag.

For UI components that are supported by HTML forms, such as button, RadioButton, Checkbutton, and other UI components, the cHTML class provides the corresponding methods, such as the RadioButtonList and Submitbutton above.

In addition to RadioButtonList, cHTML also provides a set of activexxx, such as activeradiobuttonlist, which need to be used in conjunction with Cformmodel, We did not use the model for an example of hangman, so we did not use the Activeradiobuttonlist method, which is introduced later in the model in MVC.

For all UI components supported with CHtml, refer to the Yii class reference documentation: http://www.yiiframework.com/doc/api/1.1/CHtml

Starting with version 1.1.1, a new small object cactiveform is provided. (http://www.yiiframework.com/doc/api/1.1/CActiveForm) to simplify form creation. This small object can provide both client and server-side seamless, consistent validation. These are described in conjunction with YII-supported UI components.

The above is the PHP Development Framework Yii Framework Tutorial (7) Use cHTML to create the content of the form, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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