![](/img/trans.png)
[英]yii2 hidden input field using javascript append and get it from controller
[英]Getting value of hidden input field set-in using Javascript in Yii2
我在ActiveForm
視圖中有一個隱藏的輸入字段
<?php $form = ActiveForm::begin(); ?>
<input type="file" id="i_file" name="uploadfile" value="" onchange="abc()">
<?= $form->field($model, 'path')->hiddenInput() ?>
<div class="form-group">
<?= Html::submitButton('submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
我使用Java設置它的值,如下所示:
<script>
function abc(){
var tmppath = URL.createObjectURL(event.target.files[0]);
$("#taskreports-path").val(tmppath);
alert(document.getElementById("taskreports-path").value);
}
</script>
警報顯示該值已成功設置到字段中。 現在,我想在某些php變量中輸入該輸入字段的值,例如:
$var path = //<?= $form->field($model, 'path')->hiddenInput() ?> this field's value
我如何在Yii2中做到這一點?
由於您正在使用模型來構建表單,因此最好也使用模型方法來加載數據。
$model = new MyModel();
...
if ($model->load(Yii::$app->request->post())) {
$model->path; //Your field's value should be loaded here
}
請注意,“路徑”屬性應在模型規則中列出,以便通過“加載”方法加載。 這是一個例子:
class MyModel extends ActiveRecord
{
...
public function rules()
{
return [
[['path'], 'string'],
...
];
}
}
您還可以在以下網址找到有關驗證用戶輸入的更多詳細信息:http://www.yiiframework.com/doc-2.0/guide-input-validation.html
在控制器動作的功能中,您可以通過這種方式獲取隱藏輸入的值。
$post = Yii::$app->request->post();
$var = $post[$model->formName()]['path'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.