Hello. There is an interesting challenge.

Online store. It has a product card. We go there we see detailed information on the product. Below is the id of the kits for this product. I want to implement adding to the product basket. What to eat: there is the function itself that adds the basket on the php side, but you need to add an array of id sets to this function. From here questions:

1) How to pass an array via ajax if I get it using php.

2) how to write an array transfer code? previously passed only - the name of the variable: value.

Everything is solved by jQuery

    1 answer 1

    Before sending, create an array with elements, for example:

    $('#id').click(function() { var test = []; $('.class').find(':input').each(function(i, input) { test.push(input.value()); }; }); 

    then send this variable via AJAX:

      $('#id').click(function() { var test = []; $('.class').find(':input').each(function(i, input) { test.push(input.value()); }; $.ajax({ type: "POST", url: "/test.php", data: {test: test}, dataType: "json", success: function(data) { console.log('success'); }, error: function(data) { console.log('error'); } }); }); 

    The json-array will come to your php script. Do what you want with it already :) If there are few elements in the array, you can write them into a string with a separator, transfer it as a single variable and parse it on the php side using explode.

    • , and could write as should Html. Well, more precisely to take the values ​​from Inputa? Is he alone or all values ​​separated by commas? - duddeniska
    • not quite sure what you want. Well, for example, so <form class = "inputs"> <input type = "text" value = "1" /> <input type = "text" value = "2" /> </ form> <button id = " butt "> Send </ button> - Ivan Ivanov