9

I'm trying to take input from a form and save it into a text file that is in the same folder as the html file. This is what I have so far:

<!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 does take the info from "item" and pass it to the function writeToFile() because the test alert does work. But whenever I check the file reserve.text nothing is written there. I'm very new to javascript and most of this is an amalgamation of code I saw other people using online for similar effects. Does anyone know why it is not working? Am I writing the path incorrectly? Am I not writing the script correctly?

Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46
khm
  • 466
  • 2
  • 4
  • 17
  • probably this link will help you with what you are doing [click here](http://stackoverflow.com/questions/582268/read-write-to-file-using-jquery/582281#582281) – Keppy Aug 24 '16 at 12:48
  • when you say `same folder as the html file` - I take it this is not a public facing website page? – Jaromanda X Aug 24 '16 at 12:51
  • 2
    activex is dead. throw your 20 years old books out of the window. – GottZ Oct 12 '17 at 18:50

2 Answers2

9

The problem with this is simple: Lets say a developper created javascript code to go through all your filesystem and populate it with dummy files, ruining your hard drive in the process? That is why javascript won't allow you to do this kind of operation. When we want to save information, usually, its done using server-side code and not the client's computer (unless of course we are talking about things like cookies).

The point of my answer is to let you rethink who does the saving and to where. It should be up to the server to save and retain any information for a user, and so you would not write this kind of javascript code... It is best to save data somewhere your client cannot control or edit, like on the server for instance.

I could suggest some easy PHP code, and instead of storing inside a text file, try a database... PHP is a server-side language which will let you save things to files on your server computer, however your server must be able to run PHP, most computers don't come built in with the PHP language and so you will also need a webserver with php built-in..

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35
0

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

Anyway, this code will only work on Internet Explorer, and only with the security settings way down. You shouldn't pursue this though.

If your end goal is to write a web app that stores and displays reservations, get XAMPP and find a beginner PHP + database tutorial.

  • you need to explain **why** his approach is bad. I think 99% of people here would agree with you, but your answer feels forced and closed-minded. – NoobishPro Aug 24 '16 at 14:07
  • Isn't the 2nd paragraph a perfectly good reason why this approach is bad? –  Aug 24 '16 at 14:39
  • It doesn't really explain why it's a security risk, why it only works on internet explorer and why he should use specifically PHP and/or mysql. I didn't downvote your answer btw. I'm only commenting – NoobishPro Aug 24 '16 at 15:15