Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动将可手持数据保存到JSON_Php_Jquery_Handsontable - Fatal编程技术网

Php 自动将可手持数据保存到JSON

Php 自动将可手持数据保存到JSON,php,jquery,handsontable,Php,Jquery,Handsontable,我正在使用HandsOnTable,并试图在服务器上实现保存到JSON文件 这是页面的外观: <div id="example1" class="handsontable"></div> <button name="save">Guardar</button> 我还正在编写一个PHP脚本,将上面所有单元格的数据写入一个JSON文件,只要用户单击save按钮,就会调用该文件。这就是它现在的样子: <?php if(isset($_POST['

我正在使用HandsOnTable,并试图在服务器上实现保存到JSON文件

这是页面的外观:

<div id="example1" class="handsontable"></div>
<button name="save">Guardar</button>
我还正在编写一个PHP脚本,将上面所有单元格的数据写入一个JSON文件,只要用户单击save按钮,就会调用该文件。这就是它现在的样子:

<?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);
};

?>

  • “url:”save.json“,”在ajax中没有意义,url应该是在PHP中处理发布数据的位置
  • 数据:{“data”:handsontable.getData()},(我不确定handsontable.getData()中的格式)。。。在发布之前,您可能必须将其设置为json格式的字符串
  • 在php代码中,尝试“$stringData=$\u POST[“data”];”或者在保存到文件之前,尝试使用json_encode生成json格式的字符串

  • 我也有你想做的事。我已经用Ajax和自动完成功能实现了这一点。我将新数据直接放入load.json文件中,所以每次按下load按钮,用户都会获得新数据。我的代码如下所示

    index.html

    <script data-jsfiddle="example">
    var $console = $("#example1console");
            var data = [ [] ];
            var autosaveNotification;
    
                $('#example').handsontable({
                  data: data,
                  minSpareRows: 1,
                  minSpareCols: 1,
                  colHeaders: true,
                  rowHeaders: true, 
                  autoWrapRow: true,
                  currentRowClassName: 'currentRow',
                  currentColClassName: 'currentCol',              
                  contextMenu: { items: {
                                          "row_above": {},
                                          "row_below": {},
                                          "hsep1": "---------",
                                          "col_left": {},
                                          "col_right": {},
                                          "hsep2": "---------",
                                          "remove_row": {},
                                          "remove_col": {}
                                          }
                                },
    
                  afterChange: function (change, source) 
                                {
                                    if (source === 'loadData') {
                                      return; //don't save this change
                                    }
                                    if ($('input[name=autosave]').is(':checked')) 
                                    {
                                      clearTimeout(autosaveNotification);
    
                                        $.ajax({
                                        url: "saveData.php",
                                        dataType: "json",
                                        type: "POST",
                                        data: {"data": handsontable.getData()}, //returns all cells' data
                                        complete: function (data) {
                                              $console.text('Autosaved (' + change.length + ' cell' + (change.length > 1 ? 's' : '') + ')');
                                              autosaveNotification = setTimeout(function () {
                                                $console.text('Changes will be autosaved');
                                              }, 1000);
                                            }
                                      });
                                    }
                               }
                });     
            </script>
    
    
    变量$console=$(“#示例1控制台”);
    var数据=[[]];
    var自动血管化;
    $(“#示例”).handsontable({
    数据:数据,
    会议记录:1,
    minSpareCols:1,
    colHeaders:是的,
    行标题:对,
    autoWrapRow:是的,
    currentRowClassName:“currentRow”,
    currentColClassName:'currentCol',
    上下文菜单:{项:{
    “上面第u行”:{},
    “下面第u行”:{},
    “hsep1”:“-----------”,
    “col_left”:{},
    “col_right”:{},
    “hsep2”:“-----------”,
    “删除_行”:{},
    “删除列”:{}
    }
    },
    变更后:功能(变更,来源)
    {
    如果(源=='loadData'){
    return;//不保存此更改
    }
    如果($('input[name=autosave]')。是(':checked'))
    {
    clearTimeout(自动血液透析);
    $.ajax({
    url:“saveData.php”,
    数据类型:“json”,
    类型:“POST”,
    数据:{“data”:handsontable.getData()},//返回所有单元格的数据
    完成:功能(数据){
    $console.text('Autosaved('+change.length+'cell'+(change.length>1?'s':''));
    autosaveNotification=setTimeout(函数(){
    $console.text('更改将自动保存');
    }, 1000);
    }
    });
    }
    }
    });     
    
    saveData.php

    <?php 
    
    $file = 'json\load.json'; 
    
    file_put_contents($file, ""); // Erase file content because we need to use this content for loading.
    
        foreach ($_POST['data'] as $change) 
        {       
            file_put_contents($file, json_encode($change) . ",", FILE_APPEND | LOCK_EX);
        }
    
    // remove last comma from data...
    $fileHandle = fopen($file, 'r+') or die("can't open file");
    $stat = fstat($fileHandle);
    ftruncate($fileHandle, $stat['size']-1);
    fclose($fileHandle);
    
    
    // Append rest of syntax to file content so when press load next time we got ready new data...
    $current = file_get_contents($file);
    $newData = "{ \"data\": [ " . $current . " ] }"; 
    file_put_contents($file, $newData);
    
    // success json response...
    $out = array('result' => 'ok');
    echo json_encode($out); 
    
    ?>
    
    
    
    希望这能帮助您……

    Hi,我将“save.json”替换为“escreve.php”。PHP正在创建一个空的“testFile.json”。因此,脚本正在执行,但handsontable中的数据没有被发送。谢谢你的帮助!嗨,这对我很有用:
    <?php 
    
    $file = 'json\load.json'; 
    
    file_put_contents($file, ""); // Erase file content because we need to use this content for loading.
    
        foreach ($_POST['data'] as $change) 
        {       
            file_put_contents($file, json_encode($change) . ",", FILE_APPEND | LOCK_EX);
        }
    
    // remove last comma from data...
    $fileHandle = fopen($file, 'r+') or die("can't open file");
    $stat = fstat($fileHandle);
    ftruncate($fileHandle, $stat['size']-1);
    fclose($fileHandle);
    
    
    // Append rest of syntax to file content so when press load next time we got ready new data...
    $current = file_get_contents($file);
    $newData = "{ \"data\": [ " . $current . " ] }"; 
    file_put_contents($file, $newData);
    
    // success json response...
    $out = array('result' => 'ok');
    echo json_encode($out); 
    
    ?>