Home>
var carts = [
             {name:'Product A'},
             {name:'Product B'},
             {name:'Product C'}
            ];;

var carts_json = JSON.stringify (carts);
$('# carts_hidden'). val (carts_json);
<form method = "post">
<input type = "hidden" name = "carts" value = "">
<button>Send<.button>
</form>
$carts = json_decode ($_POST ['carts'], true);
print_r ($carts);
// I don't need anything here

I want to convert a JS array to JSON, POST it, and convert it to a PHP associative array as above, but it doesn't work.
Is it possible to do the above in the first place?

[Addition]
The code is a fairly concise version of the real thing.
The method and submit of the form were missing, but it is not a problem of such a dimension, and only the value of the part converted to json is not received.
Before json_decode, for example, the following values ​​are entered, but it seems to be broken at this point in the first place, so
I would like to know if delivery is possible.

{\ "item_index \": \ "0 \", \ "name \": \ "Tomato salad \", \ "price \": \ "980 \", \ "num \": \ "2 \"}

)

  • Answer # 1

    I was able to implement it using Qs.stringify.

  • Answer # 2

    For example, method = "post" is missing in the form tag, or is it at that level?

    <? PHP
    print_r ($_ POST);
    ?>
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(function () {
      var carts = [
        {name:'Product A'},
        {name:'Product B'},
        {name:'Product C'}
        ];;
      var carts_json = JSON.stringify (carts);
      $('# carts_hidden'). val (carts_json);
    });
    </script>
    <form method = "post">
    <input id = "carts_hidden" type = "hidden" name = "carts" value = "">
    <input type = "submit" value = "send">
    </form>