1

I'm having issues using the below code to save the text input value to a .txt file. Any suggestions would be appreciated. I'm calling the function from the .js file using onclick in the last input tag. It's supposed to save the data from the second to last input tag to the .txt file. Ultimately I would like to save all values in the form, but just one value is a starting point.

<script src="../Js/Code.js"></script>
<form>
  <table>
  <tr>
  <td>
    <label for="incomeday">Date of income:</label>
    <input type="date" name="incomeday">
  </td>
  <td>
    <label for="incometype">Frequency:</label>
    <select type="list" name="incometype">
      <option>Select One</option>
      <option value="One Time">One Time</option>
      <option value="Weekly">Weekly</option>
      <option value="Bi-Weekly">Bi-Weekly</option>
      <option value="Monthly">Monthly</option>
    </td>
    <td>
      <label for="incomeamount">Amount:</lable>
      <input type="text" name="incomeamount" value="">
    </td>
    <td>
      <input type="submit" onclick="saveIncome()" value="Submit">
    </td>
  </tr>
  </table>
</form>

function saveIncome(){
  var amount = document.getElementsByName("incomeamount").value;
  var fo = fopen("../Data/Data.txt","w");
  fwrite("../Data/Data.txt",amount);
  fclose("../Data/Data.txt");
}
micogg
  • 65
  • 7
  • 1
    What are `fopen()`, `fwrite()` and `fclose()`? (Aren't they C or PHP functions?) – nnnnnn Aug 10 '17 at 03:26
  • It's not possible without user interaction. And even then, cross-browser solutions are difficult: https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server – david25272 Aug 10 '17 at 03:34
  • At Chrome, Chromium you can use `webkitRequestFileSystem` to store a file or directory at user browser configuration folder [How to Write in file (user directory) using JavaScript?](https://stackoverflow.com/questions/36098129/how-to-write-in-file-user-directory-using-javascript/). You can also use `chrome.fileSystem` to save the file directly to user file system [JavaScript/Ajax Write to File](https://stackoverflow.com/questions/42460493/javascript-ajax-write-to-file/). Else, you can offer the file for download to user. – guest271314 Aug 10 '17 at 03:57

0 Answers0