如何从javascript文件传递参数到Yii2视图?

时间:2017-03-08 11:48:16

标签: javascript php yii2

我有一个js文件,我通过它来进行ajax调用以呈现视图文件并传递一些数据。如何才能做到这一点?这是我的ajax电话

$.ajax({
        type: "GET",
        url: "index.php?r=orders/on-select",
        data: {myVar: myVar},
        success: function (data) {
            //I want to render the view here and pass the data
        },
    });

2 个答案:

答案 0 :(得分:1)

首先理解这个概念,无论你是在使用核心Php还是某些Php框架,ajax()及其工作都是一样的。

$.ajax({
    url : 'your url',
    type: 'get'  // 'get' / 'post'
    data: {
        var1 : val1,
        var2 : val2,
        var3 : val3,
        // and so on
    }
});

你可以在key:value对中传递尽可能多的参数,并在php中获取它的值,如:

$_POST['var1']$_GET['var1']

答案 1 :(得分:1)

是的你可以使用ajax call zh

来做到这一点
$.ajax({
    type: "GET",
    url: "index.php?r=orders/on-select",
    data: {myVar: myVar},
    success: function (data) {
        //I want to render the view here and pass the data
    },
});

现在在您的控制器中渲染您想要的视图并回显它

    public function actionNoSelect(){
             if(isset($_REQUEST['myVar'])){
                $html = $this->renderPartial('path to your view file',[
               'model' => $model///// passing data to your view file if you want
],true)
echo $html;
}
    }

现在在你的ajax成功功能中你可以在某些div中显示它,比如

success: function (data) {
      $('#divid').html(data);
    },