0

I have written code for appending data to csv file using javascript but data is getting stored without header name in csv file. please find below my HTML and javascript code :

<html>
<head>
   <script language="javascript">
      function WriteToFile(passForm) {
         var fso = new ActiveXObject("Scripting.FileSystemObject");
         var fileLoc = "D:\\sample.csv";
         var file = fso.OpenTextFile(fileLoc,8, true,0);
         file.writeline(passForm.FirstName.value + ',' + passForm.LastName.value);
         file.Close();
         alert('File created successfully at location: ' + fileLoc);
      }
   </script>
</head>
<body>
   <p>create a csv file with following details -</p>
   <form>
      Type your first name: <input type="text" name="FirstName" size="20">
      Type your last name: <input type="text" name="LastName" size="20">
      <input type="button" value="submit" onclick="WriteToFile(this.form)">
   </form>
</body>
</html>

Now by using above code data is stored in csv without any header name (Column name) If i append new data to csv the header name should be entered only once at the time of file creation.

Kindly help me for the same. Thank you so much in advance.

Anders Lindén
  • 6,839
  • 11
  • 56
  • 109
Vitthal Gaytonde
  • 11
  • 1
  • 2
  • 6
  • Seems obscure to create csv files like this with client side javascript. Also what happends if the textboxes named `FirstName` and `LastName` contains `,`? – Anders Lindén Jul 14 '15 at 19:16
  • I appreciate your comment but in my case First Name and Last name should be contains only alphabetic characters and numbers. None of special symbols will be used while giving input to HTML form. I just need to add Header into csv file only once at the time of file creation if not exist. Kindly help me sir for this – Vitthal Gaytonde Jul 14 '15 at 19:21

1 Answers1

0

One way would be to ensure that the code to write the header is only executed once. There are many examples on this answer.

Community
  • 1
  • 1
Robert Horvick
  • 3,966
  • 21
  • 18