1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to save a json file in my folder.

Discussion in 'JavaScript' started by rave28, Nov 5, 2019.

  1. #1
    Hi Everyone,

    i need some help how can i save this jsonfile to my folder where i have my code.

    this is the path C:\Users\robin\Downloads\xampp\htdocs\bob

    my code

           // Load initial values from JSON 'file'
        jsonfile = '[{"lane 1":"1","lane 2":"1","lane 3":"5","lane 4":"2"}]';
        var initialstate = JSON.parse(jsonfile);
        setLaneState("lane1",initialstate[0]['lane 1'])
        setLaneState("lane2",initialstate[0]['lane 2'])
        setLaneState("lane3",initialstate[0]['lane 3'])
        setLaneState("lane4",initialstate[0]['lane 4'])
    
    Code (JavaScript):

     
    Solved! View solution.
    rave28, Nov 5, 2019 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hi, it looks to me that jsonfile is just a not too long, regular string.
    Just pass it to a form, submit it and let a PHP script on your server handle where to save it?
     
    hdewantara, Nov 5, 2019 IP
  3. rave28

    rave28 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks for helping me. I'm just a noob i try to learn it. you mean with scandir()?
     
    rave28, Nov 5, 2019 IP
  4. #4
    Nope I don't think scandir() has nothing to do with this.

    If this project of yours is what I think it is, then first create a HTML form:
    <form action="process.php" method="POST">
    <fieldset>
    <input name="json" type="text">
    <button type="submit">Proceed</button>
    </fieldset>
    HTML:
    Provide a HTML button to transfer jsonfile string to form:
    <button id="save-butt" type="button">Save</button>
    HTML:
    accompanied with following javascript:
    document.getElementById('save-butt').addEventListener('click', function(){
    document.querySelector('input[name="json"]').value = jsonfile;
    }, false);
    Code (JavaScript):
    Then create a PHP script to your server (process.php) which may look like following:
    // Report all PHP errors
    error_reporting(-1);
    
    if($_SERVER['REQUEST_METHOD'] == 'POST')
      if(isset($_POST['json']){
        $json = $_POST['json'];
        file_put_contents('my.json', $json);
      }
    PHP:
    I haven't tested this all though. Good luck ;)
     
    hdewantara, Nov 5, 2019 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    In modern browsers you can force a download from JavaScript by creating an anchor with uriencoded data in its href. You add it to the DOM just long enough to Element.click() it triggering the DL, then remove it from the document.

    
    function fileDownload(filename, content, mimeType) {
    	var content = document.body.appendChild(document.createElement('a'));
    	content.download = filename;
    	content.href = 'data:' +
    		(mimeType || 'text/plain') +
    		';charset=utf-8,' + encodeURIComponent(content);
    	content.style.display = 'none';
    	content.click();
    	content.parentNode.removeChild(content);
    }
    
    Code (markup):
    Just turn your object back into a string and call the above routine.

    
    fileDownload('myJSON.json', JSON.stringifiy(jsonfile), 'application/json');
    
    Code (markup):
    that about what you're looking to do? Yeah, it requires the download dialog, but I'm assuming this is for a website and not an application? Since node.js apps do have filesystem access, you'd not be asking in the first place... or you'd use localstorage.
     
    deathshadow, Nov 5, 2019 IP
  6. rave28

    rave28 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6

    thanks for helping it works now i got 1 more question:

    i have to use ajax i think the (post methode) what i try to do is the lane 1 2 3 4 is now on 1 1 5 2 but what i want is that if i press on one button that it will be stored on the button I last clicked on. do you know how to use this and where i have to put it in de code?

      //jsonfile = '[{"lane 1":"1","lane 2":"1","lane 3":"5","lane 4":"2"}]';
        var jsonRequest = new XMLHttpRequest();
        jsonRequest.onreadystatechange = function () {
            if (jsonRequest.readyState == 4 && jsonRequest.status == 200) {
                var initialstate = JSON.parse(jsonRequest.responseText);
                setLaneStates(initialstate);
                console.log('Done loading JSON data');
            } else {
                console.log('Failed to get JSON data');
            }
        }
        jsonRequest.open( "GET", "data.json", true);
        jsonRequest.send( null );
    
    Code (JavaScript):
     
    Last edited: Nov 6, 2019
    rave28, Nov 6, 2019 IP
  7. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #7
    Sorry rave28, I reread your post many times but don't understand what you're actually up to. A click to a button will perform that ajax which reads data.json and stores it into initialState. Second click to the same button should do different thing?
     
    hdewantara, Nov 6, 2019 IP