Handsontable : datas aren't returned to the php file

269 views Asked by At

I'm french so excuse me for my english :) I checked a lot of topics. Some seems to be the same problem but it still doesn't work. I'm a beginner in javascript too.

So, here is my code : `

$(document).ready(function () {

var container = document.getElementById('Grille_competences');

var headerRenderer = function (instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.fontWeight = 'bold';
td.style.textAlign = 'center';
td.style.backgroundColor = '#B0C4DE';
};

//Initialisation des données de la grille.
var data = [["Compétences","Description","Code",""],
        ["", "", "",""],
        ["","", "",""],
        ["","", "",""],
        ["","", "",""],
        ["","", "",""]];

//Création de la grille
hot = new Handsontable(container, {
data: data,
height: 800,
width: 1183,
colWidths: [35,200,25,80],
manualRowResize: true,
colHeaders: true,
rowHeaders: true,
mergeCells: true,
stretchH: 'all',
columnSorting: true,
contextMenu: true,    
contextMenuCopyPaste: {
  swfPath: './zeroclipboard/dist/ZeroClipboard.swf'
},


//Fonctionnalités lors d'un clic droit dans la grille.

 contextMenu: {    

   items: {
    "row_above": {
        name: 'Insérer ligne au dessus',
      disabled: function () {            
        return hot.getSelected()[0] === 0;
      }
    },
    "row_below": {
        name: 'Insérer ligne en dessous',
        disabled: function() {            
        return hot.getSelected()[0] === 0;
    }
  },
    "hsep1": "---------",
    "col_left": {
        name: 'Insérer colonne à gauche',
        disabled: function () {
          return hot.getSelected()[0] === 0;
        }
    },
    "col_right": {
        name: 'Insérer colonne à droite',
        disabled: function() {
          return hot.getSelected()[0] === 0;
        }
    },
    "hsep2": "---------",
    "remove_row": {
      name: 'Supprimer la ligne',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },

    "remove_col": {
      name: 'Supprimer la colonne',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },
    "hsep3": "---------",
    "copy": {
      name:'Copier',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },
    "paste": {
      name: 'Coller',
      disabled: function(){
        return hot.getSelected()[0] === 0;
      }
    },
    "hsep4": "---------",
    "undo": {
      name:'Précédent',
      disabled: function(){
        return hot.getSelected()[0] === 0;
      }
    },
    "redo": {
      name: 'Suivant',
      disabled: function(){
        return hot.getSelected()[0] === 0;
      }
    },
    "hsep5": "---------",
    "make_read_only": {
      name: 'Lecture seule',
      disabled: function() {
        return hot.getSelected()[0] === 0;
      }
    },
    "alignment": {
      name: 'Alignement du texte',
      disabled: function () {
        return hot.getSelected()[0] === 0;        
      }          
    },
    "mergeCells": {
      name: 'Fusionner les cellules',
      disabled: function () {
        return hot.getSelected()[0] === 0;
      }
    },


    },
},


//Entetes de la grille en lecture seule. 
cells: function(row, col, prop) {
var cellProperties = {};
if(row===0){
cellProperties.renderer = headerRenderer;
}   
if(row === 0 && col <3){
       cellProperties.readOnly = true;
       }
return cellProperties;
}
});

//Lors d'un clic sur le bouton valider, transmission des données de la grille.


});

document.getElementById('save').onclick=function () {

            $.ajax({
            url: "testGetData.php",
            dataType: 'json',
            data: {'data':hot.getData()}, //retourne les données des cellules
            type: 'GET',
            success: function (data) {
                alert(data);
            },



        });
            document.location.href='testGetData.php';
    };

`

And here is the code for testGetData.php :

<?php

foreach($_GET['data'] as $value)
    echo $value;
?>

The problem is that testGetData.php seems to not receive the $_GET['data']. I tried many differents things I've seen on forums or the Handsontable's doc. I thought the cause was the scope, but I did the same as the examples (At least i believe ^^).

Can you help me please ? I don't understand what's wrong. I need a fresh look at my code.

1

There are 1 answers

2
Carsten Massmann On BEST ANSWER

The problem was not related with hansontable but with the way you sent the data using the jQuery ajax method: You specified a dataType: 'json' but the processing .php file returned a plain text format.

You should change two things, first in your .html-file:

Replace the document.getElementById('save').onclick=function () { ... } function with:

$('#save').click(function(){
  $.ajax({
    url: "testGetData.php",
    dataType: 'json',
    data: {data: hot.getData() }, 
    type: 'GET',
    success: function (retobj) { alert(JSON.stringify(retobj)); }
  });  
}

(What is the point of including the jQuery library if you don't use it ...) The stuff inside is more or less unchanged, although I changed the success() function a litle bit. It will reformat the returned JavaScript object retobj back into a JSON format again, so you will be able to see all its elements. The clever thing about the jQuery ajax()method with dataType:'json' is, that the data returned from the .php script will appear in the success() function already as a JavaScript object without you having to do anything!

And secondly - the most important thing - in your .php file:

In order to supply the success() function with the desired json formatted string the testGetData.php script needs to return some data in a json format. The easiest way of doing this is to use the php function json_encode():

<?php
echo json_encode($_REQUEST);
?>

Assuming that hot.getData() delivered an array like this one:

data= [[0,1,2,3],
       [100,101,102,103],
       [200,201,202,203]];

Then the alert function will return the following string after the #save-button was clicked (I tested it - without handsontable though):

{"data":[["0","1","2","3"],["100","101","102","103"],["200","201","202","203"]]}