Passing JSON to PHP via ajax call

Hi,

I’m trying to save my table to a database. I have the following event defined with an ajax call to my page to commit the data. The problem is the data is not in the POST variable. $_POST is set but contains no data.

           Handsontable.dom.addEvent(save, 'click', function() {
             var myData = hot.getData();
             
             $.ajax({
                 url: "test2.php",
                 data: myData,
                 dataType: 'json',
                 type: 'POST',
                 success: function(json_object) {
                   myConsole.innerText = 'Data Saved';
                 },
                 error: function(json_object) {
                   myConsole.innerText = 'Data Save Error';
                 }
             });
      
           });

I’ve echoed myData variable to myConsole and it’s getting all table data. It seems to be in CSV format though. The JQuery docs say I should be seeing the $_POST[‘data’] as an array representation of the JSON that is suppose to be passed in.

I know my test2.php file is being called. $_POST just contains no data. I’m guessing the JQuery call is bad since myData is not JSON formatted. I coudn’t find any HT API calls to reformat myData.

Thanks for any help!!
AWF

I figured it out. You need to setup the ajax call correctly so it posts data. This worked:

               Handsontable.dom.addEvent(save, 'click', function() {
                 var myData = hot.getData();
                 var myForm = new FormData();
                 myForm.append('myData', JSON.stringify(myData));
                 
                 $.ajax({
                     url: "test2.php",
                     data: myForm,
                     cache: false,
                     async: false,
                     processData: false,
                     contentType: false,
                     type: 'POST',
                     success: function(json_object) {
                       myConsole.innerText = 'Data Saved';
                     },
                     error: function(json_object) {
                       myConsole.innerText = 'Data Save Error';
                     }
                 });

               });

Glad to hear that, @awf

if you’d have any other questions feel free to open a new ticket.