0

您好,我对 javascipt 很陌生,所以请解释清楚。我目前正在运行一个 php 页面,其中包括:

upp.php

<script>
document.getElementById("data").value = localStorage.getItem('itemsArray');
</script>

这个项目数组包含这样保存的对象:

function save(){

var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];

var newItem = {};
var num = document.getElementById("num").value;

newItem[num] = {
    "methv": document.getElementById("methv").value
    ,'q1': document.getElementById("q1").value,
    'q2':document.getElementById("q2").value,
    'q3':document.getElementById("q3").value,
    'q4':document.getElementById("q4").value,
    'comm':document.getElementById("comm").value
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));}

$.post('upp.php',{ items: JSON.stringify(oldItems) }, function(response) {
    window.location.href = "upp.php";

页面的结果如下所示:

[{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}]

无论如何我可以将这些信息保存到 PHP 中并拆分数据,这样我就可以像循环或其他东西一样一次操作它。例如:

第一次:

{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}

下一个:

{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}

等等

谢谢。

4

1 回答 1

1

upp.php:

<?php 
$array = json_decode($_POST['items'], True);
foreach ($array as $key=>$line) {
    # $key is a number like 1173627548
    # and $Line is an array with methv, q1, q2, q3, q4, and comm
}
?>

这将向您显示它从 JSON 中获得的数组。现在您可以处理数据了。

于 2013-03-15T15:28:14.640 回答