0

What I want to do is get all the data from the table and send it as a autoincremented multi-dimensional array with ajax using the post method. When it gets to the server it should be like:

$_POST  -|-- ['Key1'] = 'value1'   <---This is from an HTML input
         |-- ['Key2'] = 'value2'   <---This is from an HTML input
         |-- ['tabledata']--| -- [0]-| -- ['column1'] = 'value from cell'            
                            |        | -- ['column2'] = 'value from cell'
                            | -- [1]-| -- ['column1'] = 'value from cell'
                                     | -- ['column2'] = 'value from cell'

[0] and [1] are just autoincremented numbers generated when a new array object is created representing first and second row in this case. Here is a js fiddle with what I got so far (it returns array but not the way I need them) https://jsfiddle.net/v2quhwb8/2/ with the JS:

     $(document).ready(function() {
   var myTable = $('#example').DataTable({
     responsive: true
   });



   var testData = myTable.data().toArray();

   $('#myButton').on('click', function() {

     $.ajax({
       type: 'POST',
       dataType: 'json',
       url: 'page/postTest.php',
       data: testData,
       success: function(response) {
         // console.log('Server response', response);
       }
     });

   });


 });
7
  • 1
    what is key1 and key2 and what are its values? Commented Dec 20, 2016 at 12:10
  • Key1 and Key2 come from 2 other inputs I'm sending data from. Commented Dec 20, 2016 at 12:11
  • your ['ajax'][0] must be array of column values? and here is [0] a key or a index? Commented Dec 20, 2016 at 12:17
  • No its ['ajax'][0]['colum-name']['cellvalue'] , the 0 is just the index of the array that represents the first row. Commented Dec 20, 2016 at 12:20
  • now the default is ` ['ajax'][0]` is a array of just values.. So you want a ` ['ajax'][0]` to be a object and with key value structure.. right ? Commented Dec 20, 2016 at 12:22

1 Answer 1

1

Found my own answer:

data: testData in $.ajax({... changed to data: {data: testData} and it returns the multidimensional array that I need in PHP $_POST. Also what I asked before was an associative array like the backend idiot that I am because apparently, they don't exist in javascript, my bad guys.

What you will get in PHP is something like $_POST['data'][0][0]['cell value'], the first zero is the row from first to last, the second zero represents the cells from left to right. I guess I'll have to work with this somehow. I updated https://jsfiddle.net/v2quhwb8/4/

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

Comments

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.