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

Source: Internet
Author: User
Tags yii

When you create the MVC view of a YII application, you can directly construct the page view directly using the HTML language, and Yii also provides several help (helper) classes to simplify the view writing. For example, to create a text entry field, we can call Chtml::textfield (), and to create a drop-down list, call CHtml::d ropdownlist (). Some of the view in the previous hangman example is using cHTML to create a view.

Info: You might want to know the benefits of using help classes if they require 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 will generate a text input field that can touch the publication submit action when the user modifies its value.

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

Otherwise you'll need to write a whole bunch of JavaScript.

Recall the definition of the Hangman page play:

<p>this is the game of hangman.     
You are must guess a word, a letter in a time.     
If you are 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 ();?>

The CHtml Construction page always ends with a chtml::beginform () beginning with Chtml::endform (). In fact, these two methods generate the start and end tags of the from tag.

For UI components supported by HTML forms, such as button, RadioButton, Checkbutton, and other UI components, the cHTML class provides a corresponding method, 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, For examples like hangman, we don't use model in addition, so we don't use the Activeradiobuttonlist method, and we'll introduce the model in MVC later.

You can refer to the Yii class reference documentation for all UI components supported by CHtml: http://www.yiiframework.com/doc/api/1.1/CHtml

Starting with version 1.1.1, a new small object Cactiveform (Http://www.yiiframework.com/doc/api/1.1/CActiveForm) is provided to simplify form creation. This small object provides seamless, consistent validation both on the client and on the server side. These are described in conjunction with the UI components supported by Yii.

See a full set of tutorials: http://www.bianceng.cn/webkf/PHP/201301/35265.htm

Related Article

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.