dongyuan1902 2016-10-18 20:00 采纳率: 0%
浏览 107
已采纳

如何让PHP脚本删除JSON文件中的对象?

So I have an HTML file that is dynamically populated with information from a JSON file, with an ajax _POST request.

All I'm trying to do, is grab the json (just one simple array of objects), strip out the appropriate one via it's index number passed by the ajax, and then recode the JSON back into the same file. No errors, but nothing at all is happening.

Thanks!

Here is my ajax:

$(document).ajaxComplete(function(event, xhr, settings) {
        var json = "data/comments.json";
        $('.delete').click(function(index) {
            var deleteIndex = $(this).parent().attr('id');
            var deleteIndex = parseInt(deleteIndex);
            $.ajax({
                type: 'POST',
                url: 'data/save.php', // the url where we want to POST
                data: deleteIndex,
                success: function(){ 
                                        location.reload();
                                    },
                error: function(){    
                                        alert('Fail!');
                                    }
                });
        });
    });

and here is my PHP:

<?php
$data => $_POST['deleteIndex'];
$file = file_get_contents('comments.json');
$json[] = json_decode($file, true); //return an array
foreach($json as $key => $value) {
   if($value == $data) {
    unset($json[$data]);
    file_put_contents('comments.json', json_encode($json, JSON_PRETTY_PRINT));
   }
}
?>
  • 写回答

1条回答 默认 最新

  • dqxmf02844 2016-10-18 20:28
    关注

    You didn't set the name of the value you send to your server. The data key in the request should be a {key:val} object (or a url-formatted string).

        $(document).ajaxComplete(function(event, xhr, settings) {
        var json = "data/comments.json";
        $('.delete').click(function(index) {
            var deleteIndex = $(this).parent().attr('id');
            var deleteIndex = parseInt(deleteIndex);
            $.ajax({
                type: 'POST',
                url: 'data/save.php', // the url where we want to POST
                data: {'deleteIndex': deleteIndex},
                success: function(){ 
                    location.reload();
                },
                error: function(){    
                    alert('Fail!');
                }
            });
        });
    });
    

    In your PHP code I think this will be much better:

    $data = $_POST['deleteIndex'];
    $file = file_get_contents('comments.json');
    $json = json_decode($file, true); //return an array
    
    unset($json[$data]); // I guess you want to delete the value by key
    file_put_contents('comments.json', json_encode($json));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop