0

I've created a form in html (code for that is below). I was wondering if there is a way for me to, using javascript, output the inputs of this form onto a text file on my desktop. So when someone fills in the form and presses submit, a new text file will be created with the two inputs on separate lines. I am then going to read this text file in python.

Or

if the results from the form could go straight to python instead?

<form>
    School:<br>
    <input type="text" name="schoolname"><br>
    School code:<br>
    <input type="text" name="schoolcode"><br>
    <input type="submit" value="next">
</form>
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    Send the data to server and write it there. You don't have local file access from inside browser window for obvious security reasons – charlietfl Aug 26 '18 at 12:56
  • You wont be able to do this via JS - see https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript - do you have PHP on your local machine? – Nick Duncan Aug 26 '18 at 13:01
  • @charlietfl okay thankyou, do i use javascript to send it to the server? or can html do that? –  Aug 26 '18 at 13:01
  • @NickDuncan i am using pycharm, i dont think it has PHP –  Aug 26 '18 at 13:02
  • Read up on how html forms work and also AJAX. Can send either way – charlietfl Aug 26 '18 at 13:03

1 Answers1

2

JS (running in a browser, as opposed to Node.js stuff) would be an extremely dangerous language to use if it actually allowed you to write files to drive. Imagine all the viruses that would be stored on your PC whenever you visit some certain websites.

So what you can do is send this stuff to the server. Quick and easy way would be to use jQuery for this purpose (https://api.jquery.com/jquery.post/). Quick post request to an HTTP backend that could technically run on the same machine. Then the file can be stored. In particular in Node.js, especially when using express.js, the whole server and storing of the file could be handled probably in around 20 lines of code.

Kamil Janowski
  • 1,872
  • 2
  • 21
  • 43