p5.js/ajax/php jammin' file

My program at a certain point has to save some information,and them load it in order to keep track of an user input; this is also done by multiple clients in the same time, that have to interact with the same data and see others interaction. All that to say that there is a JSON file that contains this info, that gets modified by a PHP script that is called and instructed by what it should write by P5.JS, that: reads the file, only updates the data that was changed by the user,leaving the rest akone, calls the php script. that is done about every 500 mSecs. This works fine, except some times at random the file becomes [ ] or it gets some random parenthesis at the end or beginning...

I tryed adding a way to check it the json object is empty or not well formatted, both when reading and writing the file in JS and when writing in PHP, but I'm not sure If i've done it correctly,as the file sometimes breaks again.

I'vnt been able tu understnd when the problem occurs

Answers

  • Hard for us to help without seeing any code :/

    Does the PHP script generate the JSON or does it just write the JSON provided to it by p5js to the file?

  • edited January 2016

    it uses a command that rewrites the file and,in case the file exists not, it creates it too

    var xmlhttp;
        if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
     }
     else
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        }
      }
      xmlhttp.open("POST","gg.php",true);
      xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlhttp.send("file=plugList.json&name="+JSON.stringify(jsonFile));
      console.log("done");
      console.log(varTabList);
      console.log(jsonFile);
      resetPlugChange();
    
    
      if(tabList.length!=jsonFile[0].tabList.length){
        location.reload(true);
      }
      for(this.t=0;this.t<tabList.length;this.t++){
        if(tabList[this.t].tabName!=jsonFile[0].tabList[this.t].tabName){
            tabList[this.t].tabName=jsonFile[0].tabList[this.t].tabName;
            gui[nInfos.length+this.t].name=jsonFile[0].tabList[this.t].tabName;
            windowResized();
        }
        if(tabList[this.t].plugList.length!=jsonFile[0].tabList[this.t].plugList.length){
            location.reload(true);
        }
        for(this.lP=0;this.lP<tabList[this.t].plugList.length;this.lP++){
            name=jsonFile[0].tabList[this.t].plugList[this.lP].plugName;
            state=jsonFile[0].tabList[this.t].plugList[this.lP].plugState;
            if(name!=tabList[this.t].plugList[this.lP].plugName || state!=tabList[this.t].plugList[this.lP].plugState){
                tabList[this.t].plugList[this.lP].name =name;
                tabList[this.t].plugList[this.lP].state=state;
            }   
        }
      }
    

    php part

    <?php
    $stringData = $_POST["name"];
    
    json_decode($stringData);
    
    if((json_last_error()===JSON_ERROR_NONE)==1){
    $fh = fopen($file, 'w') or die("can't open file");
     fwrite($fh,$stringData );
     fclose($fh);
    }
    ?>
    

    anyway, the problem is most surely not with the data written as js double checks that there are no problems, and php should too

Sign In or Register to comment.