post array from jQuery to yii2 session

How I can post a JavaScript array (the arrays is full with ids that are clicked), and to send him to a session that 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 IDs = [];

    s = 0;

    $('.custombtn').click(function () {

        var id = $(this).attr("value");

        IDs.push(id);

        console.log(IDs);

    });

});

And This is my Controller where I open a session , but cant figure out how the array can be stored in the session so I can use that array later .


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,

    ]);

}

You have to post the javascript array back to some controller action where you can get the post data and store it in the session.

worked with :


   $(document).ready(function () {

        var IDs = [];

        $('.custombtn').click(function () {

            var id = $(this).attr("value");

            IDs.push(id);

            console.log(IDs);

            $.ajax({

                type: 'POST',

                url: 'controllers/StockController.php',

                data: {data: IDs},

                dataType: 'json'

            });

        });

    });