1

I created a form with 4 text fields and a submit button. Now I want to store that data into a json file when I click submit. And also be able to show all the json data onto my webpage.

1 Answers1

0

Your question isn't clear, but if you want to download the json data and want to download it, here you have a snippet:

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

var obj = {"name":"Joe", "lastname":"Doe"};

download("test.json", JSON.stringify(obj));

The data filled in inputs you should save them in an object and then download that json in file using the download function.

This solution is useful if you mean to generate the json file in browser and download it. But if you mean to store that file in server, then you should consider to send these data in server using an Http request and then create and store the json file in server using your server-side language.

The function download(filename, text) was copied from: https://stackoverflow.com/a/18197341/3117111

Tedi Çela
  • 544
  • 1
  • 8
  • 20