ajax request overwrite everytime my json file

I try to save my JSON datas to a file, but it saves only the last request (I think everytime overwrite my general.json file). I would like to add all requests to general.json. Where is the problem in my codes?

the ajax part of tomorrow.php:

   $(document).ready(function () {

        var json = mg;
        for (var i = 0; i < json.length; i++) {

           $.ajax({
              type:"GET",
              url:"save_json.php",
              contentType: "application/json",
              dataType: "json",
              async: false,
              data: { data: JSON.stringify({
                  country: json[i][0],
                  competition: json[i][1],
                  club: json[i][2]}) },


              success: function(){ alert("data")},
              error: function(){ /*alert("error")*/}
            });
        }
    });

And the save_json.php:

  <?php
    $myFile = "general.json";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $_GET["data"];
    fwrite($fh, $stringData);
    fclose($fh)

    ?>

and the content of the general.json file:

 {"country":"America","competition":"Copa Americ","club":"Boca Juniors"}

Why saved only the last request? How can I save all my request without overwrite everytime general.json file?


Source: ajax

Leave a Reply