简体   繁体   English

如何通过使用AJAX和PHP进行迭代将JSON数据保存到新的JSON文件中?

[英]How to save JSON data to new JSON file through iterating using AJAX and PHP?

I have simple movies editor with the sidebar in which user just drag and drop the element to the right side user can edit this element as they wish and save data to JSON, I want when the user clicks to save it should create a new file and save to it new data. 我有一个带有边栏的简单电影编辑器,用户只需将其拖放到右侧,用户便可以根据需要编辑该元素并将数据保存到JSON,我希望当用户单击以保存它时,应该创建一个新文件并将新数据保存到其中。

so far here is what I have 到目前为止,这是我所拥有的

HTML HTML

        <div class="col-2 save-details">
          <div id="save-block">
            <button type="button" class="btn btn-success btn-save">Save</button>
          </div>
        </div>

here is js to save the file 这是保存文件的js

var moviesblocks =null;

$(document).ready(function(){

$.getJSON('moviesblocks.json', function (data) {

    moviesblocks = data.moviesblocks;
    console.log(moviesblocks);

});

$("#main-btn-save").on("click", function () {

    var moviesparams = JSON.stringify({
        moviesblocks: moviesblocks
    });

    $.ajax({
            type: 'POST',
            data: {
                moviesparams: moviesparams
            },
            url: 'save_movies_block.php',
            success: function (data) {
                    $('#msg').html(data).fadeIn('slow');
                    $('#msg').delay(2000).fadeOut('slow');
            },
            error: function () {
                    $('#error-msg').html(data).fadeIn('slow');
                    $('#error-msg').delay(2000).fadeOut('slow');
            }
    });

    console.log(moviesparams);
});

})

Here is php file to handle the process 这是处理文件的php文件

<?php
if(isset($_POST['moviesparams'])){
    $moviesparams = json_decode($_POST['moviesparams']);
    // do whatever checks you need on $moviesparams to verify valid data
    $success = file_put_contents("moviesblocks.json", json_encode($moviesparams));
    if($success === false){
        echo "sorry , something is wrong";
        die();
    }else{
        echo "Data successfully saved";
        die();
    }
} else {
    echo "nic";
}

$data=file_get_contents("data.json");
$result=json_decode($data);
?>

As you can see now after user clicks save it save to this file 如您现在所见,用户单击后将其保存到该文件

moviesblocks.json

Now I want when a user clicks save, it should save to new file something like moviesblocks1.json instead of moviesblocks.json 现在我想要当用户单击“保存”时,它应该保存到新文件中,例如moviesblocks1.json而不是moviesblocks.json。

Here is a live demo live demo 这是现场演示现场演示

How can I achieve what I want? 我怎样才能实现自己想要的?

If you dont wanna use database, you need to create one more file to maintain file version numbers. 如果您不想使用数据库,则需要再创建一个文件来维护文件版本号。 Each time you wanna create new file, get the latest version number from this version.txt file and append it to filename. 每次您想要创建新文件时,请从此version.txt文件中获取最新版本号,并将其附加到文件名中。 Eg : moviesblocks1.json, moviesblocks2.json. 例如:moviesblocks1.json,moviesblocks2.json。

After creating new file, update the version from version.txt. 创建新文件后,请从version.txt更新版本。 This will help you to create new version number next time. 这将帮助您下次创建新的版本号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM