[英]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 如何发布JavaScript数组,并将其写入在Controller中打开的会话中,这是将id保存到数组中的视图
<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 与插入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'
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.