Tianzhu
Tianzhu

Reputation: 31

export user input from HTML to a txt file using javascript only

<!DOCTYPE html>
<html>
<head>
  <title>User information</title>
  
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <form>
    
  </form>


<h1>User information</h1>

  <label>Starting: <br/>    Date : </label><input id="StartingDate" type="date"/>  Time : <input id = "StartingTime" type="time"/>

  <br/>
  <br/>

  <label>Ending: <br/>    Date : </label><input id="EndingDate" type="date"/>  Time : <input id="EndingTime" type="time"/>

  <br/><br/><br/>

  <label>Application</label>
  <select id="selector">
  <option disabled value="">Please select one</option>
  <option>NGINX</option>
  <option>HSS-FE</option>
  <option>C</option>
  </select>
 <p>Selected: <span id="selected">NGINX</span></p>

 
  <br/><br/>

<label>Parameters:</label>
<div id='example-3'>
  <input type="checkbox" id="CPU" value="CPU" v-model="checkedNames">
  <label for="CPU">CPU</label>
  <input type="checkbox" id="MEMORY" value="MEMORY" v-model="checkedNames">
  <label for="MEMORY">MEMORY</label>
  <input type="checkbox" id="LATENCY" value="LATENCY" v-model="checkedNames">
  <label for="LATENCY">LATENCY</label>
  <br>

</div>

  <br/><br/>

<button>SUBMIT</button>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

This is all I have for the front end. After the user fills in all the information(startingTime, endingTime, application, parameters) and clicks "SUBMIT", all the information will be saved and exported to a text file. I don't know how to write a javascript to address the problem. Thanks a lot for helping me out!!!

Upvotes: 1

Views: 2997

Answers (2)

Manpreet Singh Dhillon
Manpreet Singh Dhillon

Reputation: 903

You can use data URI as following:

<a href="data:application/octet-stream;utf8,test&nbsp;content">text file</a>

this will download text contents as a file.

Upvotes: 1

user871619
user871619

Reputation: 51

Answer is here -> https://www.roseindia.net/javascript/javascriptexamples/javascript-write-to-text-file.shtml

function createFile(){
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.CreateTextFile("C:\\Hello.txt", false);
file.WriteLine('Hello World');
file.WriteLine('Hope is a thing with feathers, that perches on the soul.'); 
file.Close();
}

Upvotes: 1

Related Questions