如何将数组从Javascript传递给PHP

I have a function which saves an array each time the button is clicked to localStorage.The button will be clicked multiple times and I need to put this Array list into PHP somehow which is on another page from this file.

Thanks

a.js (this function listens onLoad of the page)

    function doFirst(){
    var button = document.getElementById("button");
    button.addEventListener("click", save, false);

    var buttons = document.getElementById("clear");
    buttons.addEventListener("click", clear, false);

    var buttonss = document.getElementById("submittodb");
    buttonss.addEventListener("click", toPHP, false);

        $.ajax({
            method: 'post',
            dataType: 'json',
            url: 'edit.php',
            data: { items: oldItems }, //NOTE THIS LINE, it's QUITE important
            success: function() {//some code to handle successful upload, if needed
            }
        });

        }

        function save(){

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

        var newItem = {
           'num': document.getElementById("num").value,
            '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));}


edit.php


$parsed_array = json_decode($_POST['items']);

and i get the error: Notice: Undefined index: items in /home/project/edit.php on line 9

You can create an AJAX function (use jQuery) and send the JSON data to the server and then manage it using a PHP function/method.

Basically, you need to send the data from the client (browser) back to the server where the database hosted.

  1. Call JSON.stringify(oldItems); to create the json string

  2. Do a Do a POST request using AJAX.

Probably the simplest way is using jQuery:

$.post('http://server.com/url.php', { items: JSON.stringify(oldItems) }, function(response) {
  // it worked.
});

In order to pass this array to PHP you need to:

  1. JSon-encode it
  2. Make an AJAX or POST request to PHP
  3. Parse the passed array into PHP array

If you're using jQuery (if you're not you should start - it is really handy tool) steps (1) and (2) is as simple as

$.ajax({
    method: 'post',
    dataType: 'json',
    url: 'the URL of PHP page that will handle the request',
    data: { items: oldItems }, //NOTE THIS LINE, it's QUITE important
    success: function() {//some code to handle successful upload, if needed
    }
});

In PHP you can parse the passed array with just

$parsed_array = json_decode($_POST['items']);

There is a direct connection between { items: oldItems } and $_POST['items']. The name of variable you give to the parameter in javascript call will be the name of key in $_POST array where it ends up. So if you just use data: oldItems in javascript you'll have all your entities scattered around the $_POST array.

More on $.ajax, and json_decode for reference.