I have a JSON file as :
var info = [{
"place": "Turkey",
"username": "jhon"
}, {
"place": "Dubai",
"username": "bruce"
}, {
"place": "Italy",
"username": "Wayne"
}];
I have an HTML form as :
<!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 am trying to get the values that the user inputs in the form and add them to the JSON var that I have. But I am going wrong somewhere.Help much appreciated! So that if the user enters wayne and india in the form, my JSON array must have those two objects added to them. Plus they should reflect in the file.