0

I have a hidden input field in view in the 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(); ?>

I am setting it's value using Javascript as shown below:

<script>
function abc(){
    var tmppath = URL.createObjectURL(event.target.files[0]);
    $("#taskreports-path").val(tmppath);

alert(document.getElementById("taskreports-path").value);
}   
</script>

Alert shows that the value is set into the field successfully. Now I want the value of that input field in some php variable like:

$var path = //<?= $form->field($model, 'path')->hiddenInput() ?> this field's value

How can I do that in Yii2?

Bloodhound
  • 2,906
  • 11
  • 37
  • 71
Choxx
  • 945
  • 1
  • 24
  • 46
  • check this link http://stackoverflow.com/questions/9789283/how-to-get-javascript-variable-value-in-php – Bloodhound Jan 02 '16 at 12:17
  • 1
    assigning a value with javascript is a client operation and assignment to a variable PHP is a server-side action. The assignment of an operation to a variable server can not be done by Javascript in clinet If you want to assign a value to a variable or offettuare a server side must submit your fields in a form or make an ajax call to a server module that carries this – ScaisEdge Jan 02 '16 at 13:21
  • 1
    If your js is filling the form as expected you could just create a new rule in your model to check the attribute's value. If that will not work for you, can you explain better what's the scenario here and what you are trying to accomplish? – Clyff Jan 05 '16 at 15:48
  • If you submit the form then you can get this value in your controller in $model object. You can check all submitted field values like this. print_r(Yii::$app->request->post()); – Anamika Shrivastava Jan 11 '16 at 18:24
  • Please explain more what you want to do with hidden variable and how you want to use its value. – Anamika Shrivastava Jan 11 '16 at 18:25
  • @AnamikaShrivastava I already said, I want the value in a PHP variable in the same page. – Choxx Jan 12 '16 at 05:00
  • @scaisEdge @ Clyff Jan I got that. Thanks for the suggestion. – Choxx Jan 12 '16 at 05:02

2 Answers2

1

Since you are using Model to build your form it would be better to load data using Model method also.

$model = new MyModel();
...
if ($model->load(Yii::$app->request->post())) {
    $model->path; //Your field's value should be loaded here
}

Note that 'path' attribute should be listed in your Model rules to be loaded by 'load' method. Here is an example:

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

You can also find more details about validating user input here http://www.yiiframework.com/doc-2.0/guide-input-validation.html

Dmitriy
  • 168
  • 7
0

In your controller action's function, you can get the hidden input's value this way.

$post = Yii::$app->request->post();
$var = $post[$model->formName()]['path'];
Hanafi
  • 151
  • 6