javascript - How to add JSON objects to an array in another JSON file -
i have json
file :
var info = [{ "place": "turkey", "username": "jhon" }, { "place": "dubai", "username": "bruce" }, { "place": "italy", "username": "wayne" }];
i have html
form :
<!doctype html> <html> <body> <form onsubmit="addtojson();"> first name:<br> <input type="text" id="place"> <br> last name:<br> <input type="text" id="username"> <br><br> <input type="submit" value="submit"> </form> <script src="data.json"></script> <script> function addtojson(){ alert("inside js"); var x = document.getelementbyid('place').value; var y = document.getelementbyid('username').value; var obj = { place: x, username: y }; localstorage.setitem("info",json.stringify(obj)); } </script> </body> </html>
i trying values user inputs in form , add them json
var have. going wrong somewhere.help appreciated! if user enters wayne
, india
in form, json
array must have 2 objects added them. plus should reflect in file.
it isn't possible write arbitrary files or urls browser-side javascript.
you need bundle data in http request (using xmlhttprequest
or getting rid of javascript , submitting form normally), send http server, , have server side program (written in programming language of choice, javascript via node.js) edit file.
(editing files on servers bad idea , using database better option).
Comments
Post a Comment