我正在使用 HandsOnTable 并尝试在服务器上实现保存到 JSON 文件。
这是页面的样子:
<div id="example1" class="handsontable"></div>
<button name="save">Guardar</button>
和 jQuery:
var handsontable = $container.data('handsontable');
$parent.find('button[name=save]').click(function () {
$.ajax({
url: "save.json",
data: {"data": handsontable.getData()}, //returns all cells' data
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
$console.text('Data saved!');
}
else {
$console.text('Save error');
}
},
error: function () {
$console.text('Save error.');
}
});
});
我还在编写一个 PHP 脚本,将上面所有单元格的数据写入 JSON 文件,只要用户单击保存按钮,就应该调用它。这是它现在的样子:
<?php
if(isset($_POST['save'])){
$myFile = "testFile.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
fclose($fh);
};
?>
我的问题是如何将所有单元格的数据发送到 PHP 变量 $stringData?
提前非常感谢!