[英]ajax php post with sessions wont give results
我試圖通過ajax獲取預設的會話變量,並在我的模式中顯示。 它像一個購物車。 但是,當我嘗試獲取它們時,當我從chrome inspect network或modal讀取ajax結果時根本沒有結果。 下面是我的代碼。 我在這里做錯了什么?
<script>
$("#cart-button").click(function(){
$.ajax({
url: "includes/cart-read.php",
success: function(data){
console.log(data);
alert(data);
$('#modal-body').empty().append(''+data+'');
}
});
});
</script>
並在cart-read.php中
if(isset($_SESSION['dices'])){
foreach ($_SESSION['dices'] as $dice){
$msg = $dice;
echo json_encode($msg);
}
}
會話骰子是具有簡單數字的數組。 例如4、5、6
確保代碼中包含以下內容
嘗試使用數組來存儲和顯示結果。
<?php session_start(); $result = array(); $msg = array(); if(isset($_SESSION['dices'])){ foreach ($_SESSION['dices'] as $dice){ array_push($msg,$dice); } if(sizeof($msg) > 0) { $result['status'] = true; $result['message'] = $msg; } else { $result['status'] = false; $result['message'] = 'No values'; } } else { $result['status'] = false; $result['message'] = 'Session not set'; } echo json_encode($result); ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.