Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Save textarea input with date&time into .txt

    Hi, I am a beginner to javascript , I wish to learn how to create a .txt in local machine and store the data input in the textarea with the current data & time updated too. I am wondering is it possible to do so ?

    Please provide me a website or tutorial that I can learn about this. I tried to search it over the net but I just can't find a suitable tutorial for beginners like me. Thanks for the help. I appreaciate a lot.

    If I have post this thread in a wrong section, please kindly move it to a correct section as I can't find the suitable section to ask about this.
    Last edited by Jian0203; 03-23-2012 at 04:52 AM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript running in a web browser has no file access on the local computer.

    The only local access is to use a cookie or (with more modern browsers) use localStorage (which works similarly to cookies but isn't shared with the server and can hold a lot more info).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    JavaScript running in a web browser has no file access on the local computer.

    The only local access is to use a cookie or (with more modern browsers) use localStorage (which works similarly to cookies but isn't shared with the server and can hold a lot more info).
    Is there any alternative way to save the data and make it readable by the user(in this case refer to me) ? Because I need to save the data input by the other users with the current data as a record so that it's easier for me to trace back some information.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    ???
    Because I need to save the data input by the other users
    So how would saving the data ON THE LOCAL MACHINE help that?

    Or do you mean "by the other users OF MY MACHINE"??

    If you mean multiple users on multiple machines, then you *MUST* do this on the server. And that means using some server-side language: PHP, ASP, JSP, etc.

    If you really do mean multople users ON ONE MACHINE, then it *can* be done using Microsoft's Internet Explorer (MSIE) browser.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am developing a javascript that the application users will key in the ID of the items to retrieve the data from the server. I want to make a record either in the local machine or the server on which item data is retrieve with the current time and date. The users will retrieve the data to produce the items in production line.

    If there is any incident happens that I need to trace back the data retrieve informations(date and time), it will be easier if I keep a record of that log file.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Then *DEFINITELY* you should be doing this on the server.

    If you did on the local machine, then you would have to go around to EACH machine to collect the information. Plus, the users could alter the data, to cover up a mistake they made.

    And I would *NOT* "keep a record [in a] log file". I would keep the record IN THE SAME DATABASE (in a different table, of course) that holds the data the user is retrieving.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Jian0203 (03-22-2012)

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    And I would *NOT* "keep a record [in a] log file". I would keep the record IN THE SAME DATABASE (in a different table, of course) that holds the data the user is retrieving.
    If it's kept in the same database, I have another concern. The reason why I want to keep it in the local machine is because I want to know which computer or local machine did the error. I have many machine at different place and the machine belongs to different departments and users. If the record is kept in the same database, it will be really complicated for me to design. Btw, this record is not that important and confidential, so it's fine to save it in the local machine.

    Do you have any suggestion for my case? Thanks for helping

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Look, when you get the request from some local machine, simply record that machine's IP address, along with the time and date of the request, along with other information about the request. Store that data into a log table in the database.

    There isn't any easier way than that! Anything else will just make more work for you.

    Or if your users must login to use the system, just record their userid in the table.

    This would not be more than 10 or 15 lines of very simple code. In PHP or ASP or JSP or whatever you are using for your server-side code.

    But even if you *MUST* get data from the <textarea> of the browser, that is still simple: A tiny bit of AJAX code that sends the data to the server. Or you could do it just as easily with a hidden <iframe>. Still just a tiny bit of coding.

    ************

    Anyway, NO, there is no way to save it on the local machine UNLESS you insist that ALL users must use MSIE browsers.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Anyway, NO, there is no way to save it on the local machine UNLESS you insist that ALL users must use MSIE browsers.

    Yup, all users will ONLY and MUST use IE8. So any suggestion to save the data in local machine? Cookie? I think it's not that suitable for my case?

    Due to the strong security issue in my company, it's impossible for me to access that since I am just a trainee. I will leave the company after 2 months. I am not granted much access to database in the company. lol.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Okay, since you insist.

    Code:
    function saveToLocalFile( filename, text )
    {
        // OPTIONAL: add a timestamp to the text:
        text = "[" + (new Date()).toString() + "] " + text;
    
        // this is the magic object:
        var fso = new ActiveXObject("Scripting.FileSystemObject");
    
        // in next line, 8 means "append to end", true means create if doesn't exist
        var outfile = fso.OpenTextFile( filename, 8, true ); 
    
        outfile.WriteLine( text );
    
        outfile.Close();
    }
    So you would invoke that something like this:
    Code:
        saveToLocalFile("c:\\temp\\myLogFile.txt", document.myForm.myTextArea.value );
    Now ALL you have to do is to find a file folder (I used c:\temp) that exists on EVERY ONE of those local machines. "ALL". Hah.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Jian0203 (03-22-2012)

  • #11
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Will try to implement this later.. Thanks again for the helps ~

  • #12
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    function saveToLocalFile( filename, text )
    {
        // OPTIONAL: add a timestamp to the text:
        text = "[" + (new Date()).toString() + "] " + text;
    
        // this is the magic object:
        var fso = new ActiveXObject("Scripting.FileSystemObject");
    
        // in next line, 8 means "append to end", true means create if doesn't exist
        var outfile = fso.OpenTextFile( filename, 8, true ); 
    
        outfile.WriteLine( text );
    
        outfile.Close();
    }
    Old Pedant, I am having some problem here where the value(integer) in my textarea just won't copy into the .txt file with the date. Below is the code you provided and editted by me.

    Code:
    function saveToLocalFile( filename, text )
    {
    	var sBnr = document.getElementById("txtBnr").value;
    		
    	text = "[" + sBnr + "]" + "[" + (new Date()).toString() + "] " + text;
    
    	var fsoObj = new ActiveXObject("Scripting.FileSystemObject");
    
    	var outfile = fsoObj.OpenTextFile( filename, 8, true ); 
    		
    	outfile.WriteLine( text );
    
    	outfile.Close();
    }
    Output in .txt file : [][Thu Mar 22 16:46:51 UTC+0800 2012]

    The value such as "99995400" should be displayed in the first bracket but it's emtpy. It's hard to find the tutorial about this issue online.
    Can you please check it for me? Thanks.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Show me the <form> code you are using. Or the entire HTML page, if it's not too big.

    That code should have worked.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Mar 2012
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Show me the <form> code you are using. Or the entire HTML page, if it's not too big.

    That code should have worked.
    Code:
    </script>
    <form name = "myForm" method="post" action="">
    <table border="0" cellpadding="0" cellspacing="0" align="center" style="border: 1px solid;border-collapse: collapse;">
    	<tr bgcolor="#CAD4E3">		
    		<td width="8%"><img src="\\mkzsdn03\PO_ebd\eBondingDiagram\Imgs\infineon.jpg" height="50" width="110" /></td>
    
    		<tr><td width="8%"><b>Bnr No:</b></td>
    		<td width="15%"><input type="text" id="txtBnr" name="txtBnr" /></td></tr>
    		<td><b>Lot No:</b></td>
    		<td><input type="text" id="txtLot" name="txtLot" /></td></tr>
    		<td><INPUT id="btnCopy" TYPE="button" VALUE="Retrieve Bonding Diagram" ONCLICK="fncCopy()">&nbsp;&nbsp;<INPUT id="btnUpdate" TYPE="button" VALUE="End Lot" ONCLICK="fncUpdate()"></td>
    		<script>
    
    		saveToLocalFile("C:\\eBondingDiagram\\myLogFile.txt", document.myForm.txtBnr.value );
    		
    </script>
    	</tr>
    
    	<tr height="2px"><td colspan="4"></td></tr>
    	<tr>
    		<td colspan="4">
    			<iframe name="ifrBDiagram" id="ifrBDiagram" src=# style="border-width:4px; border-style:solid; border-color:blue;">
    				<span id="sMsg"></span>
    			</iframe>
    		</td>
    	</tr>
    </table>
    </form>
    </body>
    
    </html>
    Thanks ~

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Oh! This is easy!

    The reason this is happening is that you are calling the [b]saveToLocalFile[b] as soon as the page is loaded into the browser!!!

    BEFORE THE USER has had a chance to enter anything!

    You need to INSTEAD do the "save" call only when a button is pushed or something like that.

    EXAMPLE ONLY:
    Code:
    <input type="button" value="save to file"
        onclick="saveToLocalFile('C:\\eBondingDiagram\\myLogFile.txt', this.form.txtBnr.value );" >
    Or, if you want this to happen *AFTER* that other button you show there loads something into the field (if that is what it does), you could just make that call as the last part of your fncCopy() function.

    Or or or... But in any case, you need to wait until something is *IN* the field before trying to save it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •