...

View Full Version : Resolved Save textarea input with date&time into .txt



Jian0203
03-22-2012, 03:17 AM
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. :confused:

felgall
03-22-2012, 03:26 AM
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).

Jian0203
03-22-2012, 03:32 AM
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.

Old Pedant
03-22-2012, 03:36 AM
???

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.

Jian0203
03-22-2012, 03:44 AM
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.

Old Pedant
03-22-2012, 03:49 AM
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.

Jian0203
03-22-2012, 04:00 AM
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 :thumbsup:

Old Pedant
03-22-2012, 04:06 AM
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.

Jian0203
03-22-2012, 04:12 AM
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.

Old Pedant
03-22-2012, 04:25 AM
Okay, since you insist.



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:


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.

Jian0203
03-22-2012, 04:43 AM
Will try to implement this later.. Thanks again for the helps ~

Jian0203
03-22-2012, 11:05 AM
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.


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.

Old Pedant
03-22-2012, 10:30 PM
Show me the <form> code you are using. Or the entire HTML page, if it's not too big.

That code should have worked.

Jian0203
03-23-2012, 02:12 AM
Show me the <form> code you are using. Or the entire HTML page, if it's not too big.

That code should have worked.


</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 ~

Old Pedant
03-23-2012, 03:33 AM
Oh! This is easy!

The reason this is happening is that you are calling the 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:


<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 [b]fncCopy() function.

Or or or... But in any case, you need to wait until something is *IN* the field before trying to save it.

Jian0203
03-23-2012, 03:52 AM
Oh! This is easy!



Omg, my logic failed... keep thinking on implement this but ignore the logic when should the function takes place.. lol .. thanks a lot for the help ~~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum