Search code examples
phpyii2textinput

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


Solution

  • 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);
    
        });
    
    ');
    
    ?>