/    Sign up×
Community /Pin to ProfileBookmark

JavaScript and FORM question

Is it possible to store data collected in a ‘form’ using JavaScript?

I know I can ‘mailto’ myself by setting the ‘form action=mailto:…’, but what I would really like to do is just append the information to a local file, as in;
‘form action=’LocalFileInformation’.

Do I need to use ‘cookies’?
Am I limited to using a CGI program only for the ‘get or post’ type form?

I cannot find any information on this topic, so I must be asking for something that should not be done (?)

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@UbikDec 16.2005 — I am not sure if you are trying to save the info on the client machine (you need to use cookies for this), or your server (you will need a database and cgi for this).
Copy linkTweet thisAlerts:
@JMRKERauthorDec 17.2005 — Thanks for the information.

I'm trying to save the information to the user's computer (client?), but I thought that the amount of information that could be stored into a cookie was limited to around 1K.

What I want to do is collect information in a form and append it to a text file located on the user's computer. I expect that the amount of data I will be saving will exceed the limit of a cookie. I want to use the data collected weekly to form a database or just use the text file directly by forming a CSV format file.

Is there any program (.exe) similar to a server CGI that will store 'FORM' data locally when the 'SUBMIT' button is clicked?
Copy linkTweet thisAlerts:
@Archon_MkIVDec 17.2005 — That sounds like some scary spyware ****.
Copy linkTweet thisAlerts:
@JMRKERauthorDec 17.2005 — Sorry, I don't understand.

Spyware is not and never was my intent.

I guess my original question, reformatted is:

Is there a program available, or that I can write, that will collect information from a HTML FORM (similar to a server CGI) and store the information on the user's computer as a text file rather than sending it to the server via MAILTO: or FormMail:?

I don't want to send the information anywhere beyond the users computer. I don't want to execute any code that the user inputs. I don't want anything scary or illegal.

I just want to store information collected in a form to a file that I can import to a database or spreadsheet on a weekly basis without being connected to the server via the internet. The form would be the frontend of data collection that would be used locally only.
Copy linkTweet thisAlerts:
@UbikDec 19.2005 — No, you cannot store it in the client's machine without using a cookie.

What you are looking for may not be a web-based solution, you can develop an application that is not web based for this, and use it's own database on the client side, then maybe use XML to submit to the server every week or whatever. But you will have to install it on every client machine.
Copy linkTweet thisAlerts:
@JMRKERauthorDec 20.2005 — Thanks.

Not what I wanted to hear, but thanks anyway.
Copy linkTweet thisAlerts:
@konithomimoDec 20.2005 — Actually this can be done. JS can create/write to a predetermined file on a client's computer. It just can't read from a file or change one.
<html><head>
<script type="text/javascript">
var inputs = document.getElementsByTagName('input');
var password=inputs[0];
var name=inputs[1];

function WriteToFile() {
var filename = 'c://temp.txt';
var fso = new ActiveXObject('Scripting.FileSystemObject');
if (fso.FileExists(filename)) {
var a, ForAppending, file;
ForAppending = 8;
file = fso.OpenTextFile(filename, ForAppending, false);
file.WriteLine(name);
file.WriteLine(password);
}
else {
var file = fso.CreateTextFile(filename, true);
file.WriteLine(password);
file.WriteLine(name);
}
file.Close();
}
</SCRIPT>
</head>
<body onload='WriteToFile()'>
<form>
<input type="text" id="name">
<input type="text" id="password">
</form>
</body>
</html>
Copy linkTweet thisAlerts:
@AncoraDec 20.2005 — JMRKER:

This code (IE only, and intended to be used ONLY locally) writes/appends form text box data to a file named, Weekly Data.txt in a folder named, Saved Forms, located in the My Documents folder.

The data are separated by the vertical bar, but you can change easily change it to a comma for applications that require comma delimited info.

[CODE]<html>
<head>
<script type="text/javascript">

var fileName = "Weekly Data";
var allFieldsStr = "";

function saveIt(isForm){

allFieldsStr = "";
nFields = isForm.length;
for (i=0; i<nFields; i++)
{
tmp = isForm[i].value;
allFieldsStr += tmp+"|"; // change to a comma if desired
}
allFieldsStr = allFieldsStr.replace(/|$/,''); //change to a comma if desired
SaveForm();
}

</script>

<script type="text/vbscript">

Function SaveForm()

isData = ""+allFieldsStr+""
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists("C:Documents and SettingsAdministratorMy DocumentsSaved Forms") Then
WriteData(isData)
Set fso = Nothing
Exit Function
End If
isMessage = "The folder 'My DocumentsSaved Forms' does not exist." &vbCRLF & "Do you want to create it?"
TestVal = MsgBox(isMessage, 4+32, "Folder Does Not Exist")
If TestVal = 6 Then
fso.CreateFolder("C:Documents and SettingsAdministratorMy DocumentsSaved Forms")
WriteData(isData)
Set fso = Nothing
Exit Function
End If
isMessage = "The form data was not saved."
TestVal = MsgBox(isMessage, 0+64, "Information")
Set fso = Nothing
End Function


Function WriteData(isData)

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:Documents and SettingsAdministratorMy DocumentsSaved Forms"+fileName+".txt") Then
Set contentFile = fso.GetFile("C:Documents and SettingsAdministratorMy DocumentsSaved Forms"+fileName+".txt")
Set fsoStream = contentFile.OpenAsTextStream(8)
fsoStream.WriteLine(isData)
fsoStream.Close
MsgBox "Successfully appended to "+fileName+".txt", vbInformation, "Information"
Set fsoStream = Nothing
Set fso = Nothing
Else
Set contentFile = fso.CreateTextFile("C:Documents and SettingsAdministratorMy DocumentsSaved Forms"+fileName+".txt",True)
contentFile.WriteLine(isData)
contentFile.Close
MsgBox "Successfully saved to "+fileName+".txt", vbInformation, "Information"
set fso = Nothing
End If
End Function

</script>
</head>
<body>
<form name='Form1'>
Name <input name='personal' value="John Doe" size='25'><br>
Address <input name='address' value="123 Main Street" size='30'><br>
City <input name='city' value="Springfield" size='15'> State <input name='state' value="OH" size='2'><br>
Zip Code <input name='zip' value="12345" size='4'><br>
</form>
<input type='button' value="Save Form" onclick="saveIt(document.forms.Form1)">
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorDec 20.2005 — Wow ... two answers for the price of one question!

Thank konithomimo and Acora ... I'll give them both a shot for my local application.
Copy linkTweet thisAlerts:
@JMRKERauthorDec 20.2005 — Well, so far, I'm uncertain.

I tried the code by 'konithomimo' and it does something, but I'm not exactly sure what.

It created the file in the appropriate directory, but it appears to be a 4 byte long blank file when I look at it with 'WordPad' (?). It did not contain the information that I typed into the input form.

I tried moving the 'WriteToFile()' function to a button rather than the 'onLoad', but it only incremented the file by 4 more blank bytes. My thinking was that I needed to enter the data before it did the 'WriteToFile()' function.

Any other suggestions? It looks like it creates and appends to the file correctly with blank information at this time. So, I'll move on to the next example soon and study the first one a little longer.
Copy linkTweet thisAlerts:
@JMRKERauthorDec 20.2005 — Just tried the code from 'Ancora' and it appears to work fine. It writes the test information to the file as expected and can be viewed with my text editor.

I'll see what it takes to implement it in my own code next.

By the way, where do I learn more about the 'ActiveXObject' commands used in both sets of code? I don't think I've seen them before in any "JavaScript" book I have.

Thanks for both code though, it is much appreciated.
Copy linkTweet thisAlerts:
@AncoraDec 20.2005 — You're welcome.

But, the File System Object is not part of JavaScript. It is part of ActiveX and therefore, IE only.

It's been my experience that it's best to use VBScript when using the File System Object, and not JavaScript.

Use your favorite search engine with that phrase, in quotes: "File System Object" You'll find many resources.

Also, using my code, you may have as many form text boxes as you need. The name='something' attribute for each box is not needed.
Copy linkTweet thisAlerts:
@konithomimoDec 21.2005 — JS is a client side script, but it can be said to be used as a server side script when used with the File System Object.. You wont find any info about the FSO in a client side book.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...