Javascript Write to Textfile

I am trying to take input from a form and save it in a text file which is in the same folder as the html file. This is what I still have:

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Reservation</title> <meta name="description" content="The HTML5 Herald"> <meta name="author" content="SitePoint"> <link rel="stylesheet" href="css/styles.css?v=1.0"> <script> function writeToFile(item, name, time) { alert("Hello " + item); var fso = new ActiveXObject("Scripting.FileSystemObject"); var fh = fso.OpenTextFile("E:/labChart/etc/reserve.text", 8); fh.WriteLine(item); fh.Close(); } function readFile() { var fso = new ActiveXObject("Scripting.FileSystemObject"); var fh = fso.OpenTextFile("reserve.text", 1, false, 0); var lines = ""; while (!fh.AtEndOfStream) { lines += fh.ReadLine() + "\r"; } fh.Close(); return lines; } </script> </head> <body> Reservation <br> <form> Item: <br> <input type="text" name="item" id="item"> <br> Name: <br> <input type="text" name="name" id="name"> <br> Time: <br> <input type="date" name="time" id="time"> <br> <br> <input type="submit" value="Submit" onclick="writeToFile(document.getElementById('item').value, document.getElementById('name').value, document.getElementById('time').value)"> </form> <script src="js/scripts.js"></script> </body> </html> 

This takes information from the "item" and passes it to the writeToFile () function because the test warning works. But whenever I check the reserve.text file, nothing is written there. I am very new to javascript, and most of this is a combination of code that I saw when other people used online for similar effects. Does anyone know why it is not working? Am I writing the wrong way? Am I not writing a script correctly?

+7
javascript html
source share
2 answers

The problem with this is simple: let's say the developer created javascript code to go through your entire file system and fill it with dummy files, destroying your hard drive in the process? This is why javascript will not allow you to perform such an operation. When we want to store information, as a rule, execute it using code on the server side, and not on the client computer (unless, of course, we are not talking about things like cookies).

My answer is to let you rethink who makes the salvation and where. It should be up to the server in order to save and save any information for the user, and therefore you will not write such javascript code ... It is best to save data where your client cannot control or edit, for example, on the server for example.

I could suggest some simple PHP code, and instead of storing inside a text file, try a database ... PHP is a server language that allows you to save files to files on your server, however your server should be able to run PHP, most computers are not built into the PHP language, so you will also need a web server with built-in php.

+6
source share

In my opinion, your whole approach is bad; you need to learn PHP and mySQL to store and load persistent data. - Edit: accessing the file system with JavaScript is a huge security risk and therefore not allowed at all. If your goal is not to specifically write files from JS, there are better alternatives.

In any case, this code will only work in Internet Explorer and only with security settings. You should not pursue this, though.

If your ultimate goal is to write a web application that stores and displays reservations, get XAMPP and find the PHP + beginner's tutorial.

-one
source share

All Articles