Yii框架中的form表單

<?php
//引入命名空間
use yii\helpers\Html;
?>

<?php //表單:Html::beginForm(提交地址,提交方法,屬性數組);?>

$form = ActiveForm::begin([
    'action' => ['test/getpost'],
    'method'=>'post',
    ]); ?>

<?=Html::beginForm('','post',['id'=>'form','class'=>'form','data'=>'myself']);?>

<?php //(二)輸入框:Html::input(類型,name值,默認值,屬性數組;)?>

<?=Html::input('text','test','',['class'=>'form-control','placeholder'=>'hehe'])->hint('Please enter your test')->label('Name');?>
<?=Html::input('email','email','[email protected]',['class'=>'form-control']);?>

<?=Html::input('password','pwd','',['class'=>'form-control']);?>
<?Html::input('hidden','hidden','',['class'=>'form-control']);?>

<hr/>

<?php //Html::表單類型input(name值,默認值,屬性數值);?>

<?=Html::textInput('test','hehe',['class'=>'form-control']);?>
<?=Html::textInput('email','[email protected]',['class'=>'form-control']);?>

<?Html::passwordInput('pwd','',['class'=>'form-control']);?>
<?Html::hiddenInput('hidden','',['class'=>'form-control']);?>
<hr/>
<?php //(三) 文本域 Html::textarea()?>
<?=Html::textarea('area','',['class'=>'form-control','row'=>'3']);?>

<hr/>

<?php //單選按鈕 Html::checkbox(name值,是否選中,屬性數組)?>
<?=Html::radio('sex',true,['class'=>'form-control']);?>

<?=Html::radioList('height','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>
<?php //複選框?>
<?=Html::checkbox('haha',true,['class'=>'form-control']);?>
<?php //複選框列表?>
<?=Html::checkboxList('xixi','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

<?php //下拉列表?>
<?=Html::dropDownList('list','2',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control'])?>

<?=Html::label('顯示的','test',['style'=>'color:#ff0000']);?>
<hr/>
<?php //上傳控件?>
<?=Html::fileInput('img',null,['class'=>'btn btn-default']);?>
<hr/>
<?php //按鈕?>
<?=Html::button('普通按鈕',['class'=>'btn btn-primary']);?>

<?=Html::submitButton('提交按鈕',['class'=>'btn btn-primary']);?>

<?=Html::resetButton('重置按鈕',['class'=>'btn btn-primary']);?>

<?=Html::endForm()?>

文本框:textInput();
密碼框:passwordInput();
單選框:radio(),radioList();
複選框:checkbox(),checkboxList();
下拉框:dropDownList();
隱藏域:hiddenInput();
文本域:textarea([‘rows’=>3]);
文件上傳:fileInput();
提交按鈕:submitButton();
重置按鈕:resetButtun();

以下是代碼示例:

<?php
$form = ActiveForm::begin(['action' => ['test/getpost'],'method'=>'post',]); ?>

<? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>

<? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>

<? echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>

<? echo $form->field($model, 'edu')->dropDownList(['1'=>'大學','2'=>'高中','3'=>'初中'], ['prompt'=>'請選擇','style'=>'width:120px']) ?>

<? echo $form->field($model, 'file')->fileInput() ?>

<? echo $form->field($model, 'hobby')->checkboxList(['0'=>'籃球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>

<? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?>

<? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>

<? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>

<? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>

<?php ActiveForm::end(); ?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章