简体   繁体   中英

post array from jQuery to yii2 session

How I can post a JavaScript array , and to write him to the session I opened in the Controller This is my view where I save the id`s in an array

<script type="text/javascript">
$(document).ready(function () {
    var data = [];
    s = 0;
    $('.custombtn').click(function () {
        var id = $(this).attr("value");
        data.push(id);
        console.log(data);
    });
});

And This is my Controller where I open a session , but cant figure out how I can post the array to be stored in the session

 public function actionShop() {
    if (!Yii::$app->session->isActive) {
             Yii::$app->session->open();

        $query = Stock::find();
        $pagination = new Pagination([
            'defaultPageSize' => 6,
            'totalCount' => $query->count(),
        ]);
        $stock = $query->orderBy('id')
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();


    }


    return $this->render('shop', [
                'stock' => $stock,
                'pagination' => $pagination,
    ]);
}

worked with inserting Ajax

   $(document).ready(function () {
    var data = [];
    $('.custombtn').click(function () {
        var id = $(this).attr("value");
        data.push(id);
        console.log(data);
        $.ajax({
            type: 'POST',
            url: 'controllers/StockController.php',
            data: {data: data},
            dataType: 'json'
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM