2016-03-24 45 views
0

這是我的表單代碼:的jQuery在Yii2視圖文件

<?= $form->field($model, 'stdclass_id')->dropDownList($stdclassArray, 
    ['prompt' => 'Select','id' => 'selectedclass'])->label('Class') ?> 

<?= $form->field($model, 'session_id')->dropDownList($sessionArray, 
    ['prompt' => 'Select', 'id' => 'selectedsession',])->label('Session') ?> 

<?= $form->field($model, 'roll_no')->textInput(['maxlength' => true, 'id' => 'roll']) ?> 

這是形式的jQuery的一部分:

$script = <<< JS 

$('#selectedsession').on('change',function(){ 
    var value1 = document.getElementById("selectedclass").value; 
    var value2 = document.getElementById("selectedsession").value; 

    $.ajax({ 
     url: '<?php echo Yii::$app->request->baseUrl. '/enrollment/rollno' ?>', 
     type: "POST", 
     data: { value1: value1, value2 : value2 }, 
     success: function(data) { 
      var abc = data; 
      $("#roll").val(abc); 
     } 
    }); 
}); 

JS; 

這將產生以下錯誤:

Undefined Variable: app

我無法使用yii::$app

+0

我還包括使用yii \ helpers \ Url; –

回答

2

從這裏提取php變量doc。

$baseUrl = Yii::$app->request->baseUrl; 

$script = <<< JS 

$('#selectedsession').on('change',function(){ 
var value1 = document.getElementById("selectedclass").value; 
var value2 = document.getElementById("selectedsession").value; 

$.ajax({ 

      url: '{$baseUrl}/enrollment/rollno', 
      type: "POST", 
      data: { value1: value1, value2 : value2 }, 
      success: function(data) { 
       var abc = data; 
       $("#roll").val(abc); 
      } 
     }); 
}); 

JS; 
+0

感謝哥們。你讓我今天一整天都感覺很好 :) –

相關問題