Home > Back-end >  How to get value from textfile
How to get value from textfile

Time:11-23

I want a phone number in the text file to show in the textbox without input type="file" in the website I have a text file name phone.txt inside .txt including one phone number like 8769382349 is possible?

<input type="number" 
min="0" input mode="numeric" pattern="[0-9]*"
name="amount" id="gen-amount"
value="(value from text file)">

CodePudding user response:

You can try using fetch api

<input type="number"  min="0" input mode="numeric" pattern="[0-9]*" name="amount" id="gen-amount" value="(value from text file)">
<script>
  fetch('phone.txt').then(response => response.text()).then(text => {
    document.getElementById('gen-amount').value = text;
  });
</script>

// phone.txt

8769382349

CodePudding user response:

<input type="file" id="inputfile" oninput="read($event)" >    

read(event) {
let files = event.target.files
document.getElementById('inputfile').addEventListener('change', function () 
{
    let url = []
    var fr = new FileReader();
    fr.onload = function () {
      console.log(fr.result)
    }
    fr.readAsText(files[0]);
  })
}
  • Related