0

I am currently new to using JavaScript, especially in HTML files so please bare with me.

I am trying to take data from a HTML form, and I want to save the results from each input on a different line in a text file.

<form onsubmit="WriteResults()">
<label for="fname"><span style="font-family: Century Gothic"><span style="color: black">Forename:</span></span></label><br>
<input type="text" id="fname" name="fname" required><br><br>
<label for="email"><span style="font-family: Century Gothic"><span style="color: black">Email:</span></span></label><br>
<input type="text" id="email" name="email" required><br><br>
<input type="submit" value="Subscribe">
</form>

<script>
function WriteResults() {

const fs = require('fs');

var fn = document.getElementById("fname");
var e = document.getElementById("email");

var newString = '';

let data = newString.concat(fn,'\n',e,'\n');

fs.appendFile("results.txt", data);

}
</script>

This is the section of my code that I want to perform the actions.

The form at the top collects the data from the user, the function below should collect the data and write it to a text file.

And to process this data, I have tried many other fixes including PHP in which I have had no success, and I would prefer to use JavaScript as I have node.js installed on the server that hosts this website.

Can anyone help me come up with a solution that will allow me to collect this data and write it to a text file?

Edit: I have managed to make it so that I can save the data to the console logs, but not to a text file like I need it.

document.getElementById("subscribe").onclick = function(){

var fN = document.getElementById("fname").value;
var eM = document.getElementById("email").value;

var f = "results.txt";

let data = fN + "\n" + eM

console.log(data);

}

0 Answers0