gpt4 book ai didi

javascript - 在 Yii2 中使用 Javascript 获取隐藏输入字段设置的值

转载 作者:行者123 更新时间:2023-12-03 08:01:28 24 4
gpt4 key购买 nike

我在ActiveFormview中有一个隐藏的输入字段

<?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(); ?>

我使用 Javascript 设置它的值,如下所示:

<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
}

请注意,“path”属性应列在模型规则中,以便通过“load”方法加载。这是一个例子:

class MyModel extends ActiveRecord
{
...
public function rules()
{
return [
[['path'], 'string'],
...
];
}
}

您还可以在此处找到有关验证用户输入的更多详细信息 http://www.yiiframework.com/doc-2.0/guide-input-validation.html

关于javascript - 在 Yii2 中使用 Javascript 获取隐藏输入字段设置的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com