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):
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?
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
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.
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):
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?