0

Can I save the form data as a .txt file or .json file on my system using Fetch API in JavaScript. I dont want the form data to be saved in some PHP file

<div>
     <form>
         Name:<br>
     <input type="text" id="fname" name="firstname"><br><br>
            Phone No:<br>
     <input type="text" id="ph" name="lastname"><br><br>
            Designation:<br>
     <input type="text" id="des" name="designation"><br><br><br><br>
     <input type="button" value="Update" onclick="getdata()"</button>
            <button name="cancel">Cancel</button>
     </form>
</div>
<script>
function getdata(){
            var name = document.getElementById('fname').value; 
            var ph = document.getElementById('ph').value; 
            var des = document.getElementById('des').value;  
            alert(name);
      }
</script>

Aksen P
  • 4,564
  • 3
  • 14
  • 27

1 Answers1

1

No, you can not, FetchAPI is HTTP client and you need server to receive your HTTP request you sent with Fetch and save data from that request to file. It can be HTTP server written in any language: php, node, c#, etc.

You can, however, prompt user to download text file you generated via JS in browser. If it is what you need, you'll need to refer to FileReader API and/or change your question

Danila Alpatov
  • 822
  • 7
  • 11