0

This is my form code:

<?= $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']) ?>

And this is the jQuery part of the form:

$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;

This produces the following error:

Undefined Variable: app

I'm not able to use yii::$app.

1
  • I also included use yii\helpers\Url; Commented Mar 24, 2016 at 11:23

1 Answer 1

2

Extract php variables from here 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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy. You made my day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.