在cookie中传递JS数组并在PHP中检索

时间:2014-06-11 11:40:04

标签: javascript php arrays cookies

在我的Javascript我有array名为productArray。当我做console.log时,它看起来像这样:

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个是具有股票代码和数量的产品,第3个是与正在进行的订单相关的注释。

我想知道如何将此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)

因此我无法在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";
    }
    }

有没有人知道我如何将JS Array加入Cookie,以便我可以在PHP脚本中检索它?

这是我的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: '/' });
});

1 个答案:

答案 0 :(得分:0)

也许你在js中使用了错误的编码。 试试这个:

$encoded = utf8_encode($_COOKIE['order_cookie']);
$orderArray = json_decode($encoded, true);