Mustafa
Mustafa

Reputation: 95

yii2 How can I get the textInput value and send to another textInput

I am new for Yii2 and I have a question.

In my _form.php (generated automatically by gii) I have two fields (textInput)

<?= $form->field($model, 'Test1')->textInput() ?>
<?= $form->field($model, 'Test2')->textInput() ?>

I want to get Test1's (textInput) value and get Test2's value, and send the total value to another textInput.

<?= $form->field($model, 'Test3')->textInput() ?>

I want to see the total value in the Test3 (textInput).

Can anyone help me please..

Upvotes: 0

Views: 1648

Answers (2)

Double H
Double H

Reputation: 4160

This can be done by adding custom javascript in your view

<?php
$this->registerJs('

    jQuery(document).on("change" ,"#'. Html::getInputId($model ,'Test2') .'" ,function(){
        $("#'. Html::getInputId($model ,'Test3') .'").val();
        var first = $("#'. Html::getInputId($model ,'Test1') .'").val();
        var second = $("#'. Html::getInputId($model ,'Test2') .'").val();
        var third = first +"  " + second;
        $("#'. Html::getInputId($model ,'Test3') .'").val(third);

    });

');

?>

Upvotes: 1

Patryk Radziszewski
Patryk Radziszewski

Reputation: 511

  1. Add Test3 to your model as property.
  2. Create getter

public function getTest3() { return $this->Test1 . " " . $this->Test2; }

Upvotes: 1

Related Questions