简体   繁体   中英

Fetch API in JavaScript to Save Form Data to a JSON or TEXT File

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM