0

Hello i am quite new to javascipt so please explain things clearly. I am currently running a php page which includes:

upp.php

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

this items array contains objects which is saved like this:

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";

the result of the page appears like this:

[{"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":""}}]

is there anyway i can save this information into PHP and split the data so I can manipulate it one at a time like a loop or something. For example:

1st time:

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

Next:

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

etc.

Thanks.

1
  • 1
    in upp.php you can use the json_decode function to turn it back into objects Commented Mar 15, 2013 at 15:27

1 Answer 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
}
?>

That will show you the array it got from the JSON. Now you can work with the data.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey David i am getting a Undefined Index in my php page for the items. Any ideas? or do i have to import something like jQuery?
Test your jQuery AJAX call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.