簡體   English   中英

如何將參數從javascript文件傳遞到Yii2視圖?

[英]How to pass parameters to a Yii2 view from javascript file?

我有一個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
        },
    });

首先要了解這個概念,無論您是在核心Php還是某些Php框架上工作,ajax()及其工作方式都一樣。

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

您可以根據需要在key:value pair中傳遞盡可能多的參數,並在php中獲取其值,例如:

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

是的,您可以使用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
    },
});

現在在您的控制器中渲染所需的視圖並回顯它

    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);
    },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM