0

In salary form I want to get data from ratechart model in certain textfields when the form loads.

In Ratechart Controller I've added

public $rateid = '1';

public function actionGetForRatechart($rateid)
    {
        $rates = Ratechart::find()->where(['rc_id'=> $rateid])->asArray()->one();
        echo Json::encode($rates);
    }

In salary _form I've added the following javascript code -

<?php
$script = <<<EOD
$(window).load(function(){
    $.get('index.php?r=salary/ratechart/get-for-ratechart',{ rateid : rateid }, function(data){
    alert(data);
    var data = $.parseJSON(data);
});
EOD;
?>

It's not giving any result. It's the same question as Load data to a form from a different model in yii2. Please help.

Community
  • 1
  • 1
Alias
  • 611
  • 13
  • 42

1 Answers1

1

Dear you forgot to registerJs() in view. and your js syntax is wrong.please use this.

<?php

$script = <<<EOD
  $(window).load(function(){
  $.get(
  'index.php?r=salary/ratechart/get-for-ratechart',
  { rateid : 1 }, 
  function(data){
      alert(data);
      var data = $.parseJSON(data);
  }
  );
});
EOD;

$this->registerJs($script);
?>
Double H
  • 3,606
  • 1
  • 13
  • 25