I have a txt file, like this:
Test 1 “SCH4U , BSK4U1” [email protected] 12 9:00 – 12:00
Test 2 MPH4U [email protected] 10 10:00 – 12:00
Want to know, how to reference the text file, through code and then display it on the webpage. I am using wordpress for this. I can’t use any plugins as the admin does not allow any.
<form id="myForm">
<input type="file" id="txtFile" accept=".txt" />
<br />
<input type="submit" value="Submit" />
</form>
<script>
const filePath = 'https://hwdsbcommons.s3.amazonaws.com/wp-content/uploads/sites/16166/2024/05/testtutor.csv_.txt';
const myForm = document.getElementById("myForm");
const txtFile = document.getElementById("txtFile");
myForm.addEventListener("submit", function (e) {
e.preventDefault();
const input = txtFile.files[0];
//const input = filePath;
const reader = new FileReader();
const i = 0;
reader.onload = function (e) {
const text = e.target.result;
const Text = [];
Text[i] = text;
document.write(Text[i]);
i = i + 1;
};
reader.readAsText(input);
});
</script>
That is my current code, but instead of a button, and user input, I need it to do it from a static file path. The file name stays the same but the file is updated from time to time.
Parth Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.