1

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));
   }
}
?>
4
  • 2
    that's hideously inefficient. why do you want to keep re-writing the json file on every loop iterating? read the json, decode it, manipulate the in-memory data, THEN write out the new json once you're done manipulating. Commented Oct 18, 2016 at 20:02
  • use $json = json_decode($file, true); i.e. not $json[] Commented Oct 18, 2016 at 20:02
  • 1
    Why JSON_PRETTY_PRINT its code that is playing with the data, it does not need to be pretty Commented Oct 18, 2016 at 20:03
  • Thanks, both good points. Commented Oct 19, 2016 at 1:04

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

5 Comments

I tried the above, and it's definetly passing something to the php script, but it's actually writing into the json file, rather than deleting anything.
What it writes in is
Ugh, SO noob here... it writes in a new JSON object with "NULL" for every entry.
glad I could help :)
Nevermind I just figured out what it wasn't working. My screwup is too embarrassing to even mention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.