0
在我的Javascript
中,我有一個名爲productArray的array
。當我做了console.log
它看起來像這樣:在cookie中傳遞一個JS Array並在PHP中獲取
Object, Object]
0: Object
quantity: "3"
stockCode: "CBL202659/A"
__proto__: Object
1: Object
quantity: "2"
stockCode: "CAV BOX SGLE BR "
__proto__: Object
2: Object
comment: "This is a Test Comment"
__proto__: Object
length: 3
__proto__: Array[0]
正如你可以看到它包含3個對象:第2產品,股票代碼和數量,第三是與所取得的訂單相關聯的註釋。
我想知道如何將這個array
放到cookie
中,以便我可以在排序過程中稍後從php
腳本中輕鬆找回它。
目前,我這樣做:
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
和PHP
我這樣做是爲了獲得參考:
$encoded = json_encode($_COOKIE['order_cookie']);
$orderArray = json_decode($encoded, true);
我array
最終被作爲String
輸出,當我做到這一點被認爲是var_dump($orderArray)
string '[{\"stockCode\":\"CBL202659/A\",\"quantity\":\"3\"},{\"stockCode\":\"CAV BOX SGLE BR \",\"quantity\":\"2\"},{\"comment\":\"This is a Test Comment\"},{\"comment\":\"\"}]' (length=168)
and as因此我無法在foreach
循環遍歷它:
if(is_array($orderArray)){
echo "IS ARRAY";
foreach($orderArray as $item){
if(!array_key_exists('comment', $item)){
$orderContent .= "Stock Code: " . $item['stockCode'] . " Qty: " . $item['quantity'] . "<br/>";
}else{
$orderContent .= "Comments: " . $item['comment'];
}
}
echo $orderContent;
}else{
echo "NOT ARRAY";
}
}
有誰知道我怎麼能在這樣我可以在PHP
腳本檢索的方式得到這個JS
Array
到Cookie
???
這是我JS Script
if($.cookie('order_cookie') != undefined){
productArray = JSON.parse($.cookie('order_cookie'));
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
}
//Reference to the order table
var ordertable = document.getElementById("ordertable");
//Loop through the Array and display in the table
for(var i = 0; i < productArray.length; i ++){
// console.log(productArray[i]);
console.log("Order Item " + i);
console.log("StockCode: " + productArray[i].stockCode);
console.log("Quantity: " + productArray[i].quantity);
var row = ordertable.insertRow(i + 1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = productArray[i].stockCode;
cell2.innerHTML = productArray[i].quantity;
cell3.innerHTML = "<input type='button' value='-' class='removeBtn'/><input type='button' value='+' class='addBtn'/><input type='button' value='Delete' class='deleteBtn'/>"
}
//Delete Button
$(".deleteBtn").click(function(){
var row = this.parentNode.parentNode;
var rowToDelete = row.rowIndex;
var elementToDelete = row.rowIndex-1;
//Remove from Array
productArray.splice(elementToDelete,1);
//Remove from Table
ordertable.deleteRow(rowToDelete);
//Update the Cookie with the information every time you delete
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
//Change the total
$('.removeBtn').click(function(){ //Remove 1 from quantity
var row = this.parentNode.parentNode;
var elementToUpdate = row.rowIndex - 1;
if(productArray[elementToUpdate].quantity <= 1){
ordertable.deleteRow(row.rowIndex);
productArray.splice(elementToUpdate,1);
}else{
productArray[elementToUpdate].quantity--;
ordertable.rows[row.rowIndex].cells[1].innerHTML = productArray[elementToUpdate].quantity;
}
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
$('.addBtn').click(function(){ //Add 1 to quantity
var row = this.parentNode.parentNode;
var elementToUpdate = row.rowIndex - 1;
productArray[elementToUpdate].quantity++;
ordertable.rows[row.rowIndex].cells[1].innerHTML = productArray[elementToUpdate].quantity;
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
$('.confirmBtn').click(function(){
//Get reference to the Value in the Text area
var comment = $("#comments").val();
//Create Object
var orderComment = {
'comment' : comment
};
console.log(productArray);
//Add Object to the Array
productArray.push(orderComment);
//update cookie
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
你在做'JSON.stringify'兩次嗎?你能展示更多的JS代碼嗎? – Halcyon
我試圖實現的是當點擊一個'button'時,對訂單進行了更改,並且'cookie'在更改之前已經更新,然後發送給'php'處理。 – Javacadabra
爲什麼調用'json_encode $ _COOKIE [ 'order_cookie'])'?它已經是json編碼的字符串。所以你編碼兩次 – hindmost