php yii 中的基本隐藏字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18944326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:32:20  来源:igfitidea点击:

Basic Hidden field in yii

phpyiifieldhiddenyii1.x

提问by marchemike

I'm trying to place data in hidden text in yii, but I don't know how. I need a similar code to a regular php syntax:

我试图在 yii 的隐藏文本中放置数据,但我不知道如何。我需要一个与常规 php 语法类似的代码:

<input type="hidden" name="field_name" value="a"/>

It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.

它应该是一个静态值为 a 的字段。我只需要它与我的 $_POST 变量一起进行错误检查。

Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.

是否可以避免修改模型和控制器只是为了将字段放入?我不能使用 gii 因为我只有代码片段。对不起,我对 yii 知之甚少,所以我不知道我是什么我说最后两句话是正确的。

回答by Developerium

in views

在视图中

hidden field withmodel and form:

带有模型和形式的隐藏字段:

<?php echo $form->hiddenField($model, 'name'); ?>

or withoutmodel

没有模型

<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>

回答by Alireza Fallah

Yii hidden input :

Yii 隐藏输入:

<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>

回答by Tchaps

if data from database and value or size field:

如果来自数据库和值或大小字段的数据:

echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>

回答by johnsnails

Yii 1

<?php echo $form->hiddenField($model, 'name'); ?>

Yii2

Yii2

<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>

Also, worth noting for Yii2, the array parameter works different to a normal form field. E.G. A normal input would look more like this.

另外,值得注意的是 Yii2,数组参数的工作方式与普通表单字段不同。EG 正常输入看起来更像这样。

<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>

Hope this helps.

希望这可以帮助。

回答by Shuhad zaman

for yii2 you can try this

对于 yii2 你可以试试这个

<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>

It worked for me

它对我有用

回答by happyhardik

Alternatively,

或者,

echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));

This would set hidden field value as the id from model. The [$i] is useful for multiple record update.

这会将隐藏字段值设置为模型的 id。[$i] 对于多记录更新很有用。

回答by Asfandyar Khan

Here are two ways to do that...

这里有两种方法可以做到这一点......

without model

无模型

echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));

with model

带模型

echo $form->hiddenField($model, 'name');