Jump to content

Trying to create HTML form that will read/write to CSV.


0phoi5

Recommended Posts

Quick question.

Why does this not save when var fileLoc is set to a network location, but works fine when I put C:\somewhere?

<html>

<head>

<script language="javascript">

function WriteToFile(passForm) {

var fso = new ActiveXObject("Scripting.FileSystemObject");

var fileLoc = "\\SERVERNAME\Folder 1\Folder 2\data.csv";

var file = fso.CreateTextFile(fileLoc, true);

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>

Initial question resolved. Please read below thread for further question/issue.

Thanks.

Edited by haze1434
Link to comment
Share on other sites

Because you need to log in to a network location?

Sorry Cooper, I don't understand.

I am signed on to Windows as JOEBLOGGS, using Internet Explorer as JOEBLOGGS, and trying to save to \\SERVERNAME\Folder 1\Folder 2\data.csv, which JOEBLOGGS has Modify access to.

Edited by haze1434
Link to comment
Share on other sites

Ah, got it working with;

\\SERVERNAME\\Folder 1\\Folder 2\\data.csv

Apparently, network locations have to have double backslashes.

Another quick query. This works great for amending the first line of data.csv over and over, but how would I amend the script to add data to a new line, each time the user clicks submit?

Thanks.

Edited by haze1434
Link to comment
Share on other sites

So far, I have the below (and yes, it's rather messy as work-in-progress).

If there's a better way, I'm all ears, as this is unknown territory for me.

The idea behind the form is that it will be used as a 'backup' for logging IT tickets, in-case our normal website is down. So it needs to be in a shared folder, not on a hosted website. I am therefore unable to use PHP etc. Needs to be pretty much client-side script, except it will be saved on a shared drive.

I wanted to use CSV and HTML because CSV files are nice and easy to pull to other programs, easily shareable across the business, and HTML makes the form nice and user-friendly and pretty.

Cheers.


<html>
    <head>
 
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
 
 
 
  <title>TITLE</title>
 
 
 
      <link rel="stylesheet" href="CSS/style.css">
    

    
    
    
    

<!-------------------------------------------------
 Javascript to save form data to CSV file
-------------------------------------------------->    
 
  <script language="javascript">
    function WriteToFile(passForm) {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fileLoc = "\\SOMEPLACE\\SOMEWHERE";
      var file = fso.CreateTextFile(fileLoc, true);
      file.writeline(
                        passForm.Username.value + ',' +
                        passForm.Hostname.value + ',' +
                        passForm.Description.value + ',' +
                        passForm.Resolvers.value
                    );
      file.Close();
      alert('Ticket saved.');
     }
  </script>
 

 
 
 
 
 
 
 
    </head>
    
    
    
        <body>
  <p>TITLE</p>
  <form>
 
    User's Name: <input type="text" name="Username" size="20">
    
            <br /><br />
 
    Hostname: <input type="text" name="Hostname" size="20">
    
            <br /><br />    
    
    Description: <input type="text" name="Description" size="20">
    
            <br /><br />    
    
    Resolver Group:    <select name="Resolvers">
                            <option value="IT Service Desk"> Team 1 </option>
                            <option value="IT Desktop"> Team 2 </option>
                            <option value="IT Technical Application Support"> Team 3 </option>
                    </select>
                    
            <br /><br />                    
 
    <input type="button" value="Save Ticket" onclick="WriteToFile(this.form)">
  </form>
 
 
              <hr />
 
 
 
 
 
<!-------------------------------------------------
 Show the contents of the CSV file on the webpage.
-------------------------------------------------->
 
<table datasrc='#data'>
 
  <thead>
     <tr><th>Ticket Ref.</th><th>Date</th><th>User</th><th>Issue</th></tr>
  </thead>

 
 
<!-----------------------------------------------------------------------
 The dataflds need to have the same name as the columns in the CSV file
------------------------------------------------------------------------>
  <tbody> <tr>
     <td><span datafld='Ticket Ref'> </span></td>
     <td><span datafld='Date'></span></td>
     <td><span datafld='User'> </span></td>
     <td><span datafld='Issue'> </span></td>
  </tbody>

</table>


<!-------------------------------------------------------------------------------------------------------------------------
The ActiveX control is initialized using the <object> tag. The CLASSID (unique identifier) for the tabular data control is
CLSID:333C7BC4-460F-11D0-BC04-0080C7055A83. This needs to stay the same, else this table won't work.
-------------------------------------------------------------------------------------------------------------------------->

<object ID=data classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83">
  <param nAme="DataURL" value="CSV/DRdata.csv">
  <param nAme="UseHeader" value="true">
</object>
 
 
 
 
 
        </body>
</html>
Edited by haze1434
Link to comment
Share on other sites

Yes haze I think that's going to have to be good enough. Since it's ActiveX it only works in IE is that correct? In any case of this working the javascript will have to call out to a plugin or extension of some sort since js can't save files directly without a download prompt and the user would have to know where to save it, or with a suppressed prompt it would just save to their downloads folder and not be put in the right place.

Are the double slashes needed because that's what it expects when saving to a network location, or is it simply because the ActiveX plugin is written in C/C++ where backslashes need to be doubled because they need to be escaped? I think that might be the case.

Example: for windows code I have to make sure to have the double backslashes in paths

//win
string winPath = "C:\\Users\\Alf\\Documents\\file.csv";
//lin
string linPath = "~/Documents/file.csv";

I'm going to bookmark this thread under "Reasons ActiveX must die."

If the above is the reason for the double blackslashes I wouldn't blame activex even though yes I agree haha it should just go away already, along with flash!! (however it is helping haze here in one of these last dieing use cases) I would blame Windows for choosing to go with blackslashes for paths rather than the way cooler forward slashes /. Did they do that just to be different?

Anyway I don't mean to hijack this thread, but it's already solved pretty much, but I have a small forward slash / back slash \ dilemma here: How should I handle paths in a multiplatform application namely windows Vs everything else...

I have two solutions and which one do you think would be best?

1. Check if the platform is Windows and if so set a slashType variable to "\\" (double backslash / escaped blackslash) else set it to "/" (single forwardslash) then in each path I create I would have to append that variable in place of where there would be slashes which would kind of be a bit of a hassle. Ex. (string filePath = appDir + slashType + fileDir + slashType + fileName + ".csv";)

2. Create a correctSlashes function / class member that will just either modify the string or return a new string with the proper slashes depending on the platform the application is running on. So if platform is linux the function will go through the entire string and replace a double backslash with forward slashes or if it's Windows it will replace forward slashes with double backslashes.

I'm thinking the second option is better. However maybe there's already something that does this built in to the standard library or the dev environment I'm using, so I'd want to use that if there is. What do you guys think I should do, or know of an already existing way that handles this?

Link to comment
Share on other sites

I would blame Windows for choosing to go with blackslashes for paths rather than the way cooler forward slashes /. Did they do that just to be different?

Because IBM tools used is as a switch character and its use was already ingrained within the 'platform' (we're talking DOS here) Microsoft chose to go for the \ char because it was the most similar-looking one to the UNIX path separator /.

http://www.howtogeek.com/181774/why-windows-uses-backslashes-and-everything-else-uses-forward-slashes/

Link to comment
Share on other sites

Thanks guys :)

How would one go about amending...

    function WriteToFile(passForm) {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fileLoc = "\\SOMEPLACE\\SOMEWHERE";
      var file = fso.CreateTextFile(fileLoc, true);
      file.writeline(
                        passForm.Username.value + ',' +
                        passForm.Hostname.value + ',' +
                        passForm.Description.value + ',' +
                        passForm.Resolvers.value
                    );
      file.Close();
      alert('Ticket saved.');
     }

... to save to a new line in the CSV file on each save, rather than overwriting the first line?

Thanks.

Link to comment
Share on other sites

new Date().toString()

Apologies, I don't follow your logic. This would give me the current date and time as a variable? How would I then use this to tell the script to add a new line in the CSV file?

Edited by haze1434
Link to comment
Share on other sites

I would make a new csv file.

To append to the file, don't invoke CreateTextFile but invoke OpenTextFile.

Edited by cooper
Link to comment
Share on other sites

I would make a new csv file.

To append to the file, don't invoke CreateTextFile but invoke OpenTextFile.

Ah, thank you :smile:

Working great using;

function WriteToFile(passForm) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var fileLoc = "J:\\~Business Systems Support\\Customer Delivery\\Service Support\\Disaster Recovery\\CSV\\DRdata.csv";
        var file = fso.OpenTextFile(fileLoc, 8);
        file.writeline(
                        Date() + ',' +
                        passForm.Username.value + ',' +
                        passForm.Hostname.value + ',' +
                        passForm.Title.value + ',' +
                        passForm.Resolvers.value + ',' +
                        passForm.Priority.value + ',' +
                        'Open'
                    );
        file.Close();
        alert('Ticket saved.');
        location.reload();
        }
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...