search

form in Yii framework

Dec 01, 2017 am 09:30 AM
formyiiform

Students who have used the yii framework all know that the form form in the yii framework can be submitted using the yii internally defined form component. The editor today Let's take a look at the formform component in yii!

Without further ado, let’s talk about the code:

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

<?php //表单:Html::beginForm(提交地址,提交方法,属性数组);?>

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

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

<?php //(二)输入框:Html::input(类型,name值,默认值,属性数组;)?>

<?=Html::input(&#39;text&#39;,&#39;test&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;,&#39;placeholder&#39;=>&#39;hehe&#39;])->hint(&#39;Please enter your test&#39;)->label(&#39;Name&#39;);?>
<?=Html::input(&#39;email&#39;,&#39;email&#39;,&#39;admin@admin.com&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>

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

<hr/>

<?php //Html::表单类型input(name值,默认值,属性数值);?>

<?=Html::textInput(&#39;test&#39;,&#39;hehe&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>
<?=Html::textInput(&#39;email&#39;,&#39;admin@admin.com&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>

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

<hr/>

<?php //单选按钮 Html::checkbox(name值,是否选中,属性数组)?>
<?=Html::radio(&#39;sex&#39;,true,[&#39;class&#39;=>&#39;form-control&#39;]);?>

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

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

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

<?=Html::submitButton(&#39;提交按钮&#39;,[&#39;class&#39;=>&#39;btn btn-primary&#39;]);?>

<?=Html::resetButton(&#39;重置按钮&#39;,[&#39;class&#39;=>&#39;btn btn-primary&#39;]);?>

<?=Html::endForm()?>


##Text box: textInput(); Password box :passwordInput();
Radio box:radio(),radioList();
Checkbox:checkbox(),checkboxList();
Dropdown box:dropDownList();
Hidden field :hiddenInput();
Text area: textarea(['rows'=>3]);

File upload:fileInput(); Submit button:submitButton();
Reset Button:resetButtun();

The following is a code example:

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

<? echo $form->field($model, &#39;username&#39;)->textInput([&#39;maxlength&#39; => 20]) ?>

<? echo $form->field($model, &#39;password&#39;)->passwordInput([&#39;maxlength&#39; => 20]) ?>

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

<? echo $form->field($model, &#39;edu&#39;)->dropDownList([&#39;1&#39;=>&#39;大学&#39;,&#39;2&#39;=>&#39;高中&#39;,&#39;3&#39;=>&#39;初中&#39;], [&#39;prompt&#39;=>&#39;请选择&#39;,&#39;style&#39;=>&#39;width:120px&#39;]) ?>

<? echo $form->field($model, &#39;file&#39;)->fileInput() ?>

<? echo $form->field($model, &#39;hobby&#39;)->checkboxList([&#39;0&#39;=>&#39;篮球&#39;,&#39;1&#39;=>&#39;足球&#39;,&#39;2&#39;=>&#39;羽毛球&#39;,&#39;3&#39;=>&#39;乒乓球&#39;]) ?>

<? echo $form->field($model, &#39;info&#39;)->textarea([&#39;rows&#39;=>3]) ?>

<? echo $form->field($model, &#39;userid&#39;)->hiddenInput([&#39;value&#39;=>3]) ?>

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

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

<?php ActiveForm::end(); ?>

The above is all the content of this chapter, I hope it will bring you help.

Related recommendations:

How to load the verification code function that comes with Yii

Detailed explanation of the method of leaving the current page after adding, deleting, modifying and checking in Yii2

Using Yii form model and submitting form data in array form_PHP tutorial

The above is the detailed content of form in Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor