Click to See Complete Forum and Search --> : writing to file with java script


saphiroth
01-02-2006, 12:25 PM
Hello,
I'm trying to have a user input their name on a webpage and then have the webpage store that name in a text file on the server. I have a form on a webpage where a user types their name in and then after pressing submit the name gets saved in a text file. But for some reason this does not work for me. Please help!! Here is my code.

<SCRIPT LANGUAGE="JavaScript">
function WriteToFile(passForm) {

set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("C:\test.txt", True);
s.writeline(passForm.Firstname.value);
s.writeline(passForm.LastName.value);
s.writeline("-----------------------------");
s.Close();
}
</SCRIPT>

</head>

<body>
<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form onSubmit="WriteToFile(this)">
Type your first name:
<input type="text" name="FirstName" size="20">
<br>Type your last name:
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit">
</form>

felgall
01-02-2006, 02:17 PM
Javascript has no access to the server and no access to write files at all (except for cookies).

The code that you have there is JScript code which Internet Explorer can use to write to files on the client computer on intranets.

saphiroth
01-03-2006, 07:21 AM
Actually I am on the intranet and the webpage is going to be runing from my computer so the text file will be saved on my macine, but this still wont work?
Thank you

JPnyc
01-03-2006, 07:44 AM
Won't work the way you have it there. You need to instantiate an ActiveXObject to do what you're trying to. Can't you use VBscript for this? Would be easier.

wav syntax
01-07-2006, 07:59 PM
Your <script> tag's language attribute says JavaScript but your syntax is VBScript,
which is most likely causing the error.

Here's the code you want to use if you're going to write it in JavaScript (Actually JScript in this case) syntax...

<html>
<head>

<script type="text/javascript">
function WriteToFile(passForm) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\test.txt", true);
s.writeLine(passForm.Firstname.value);
s.writeLine(passForm.LastName.value);
s.writeLine("-----------------------------");
s.Close();
}
</script>


</head>
<body>
<p>To sign up for the Excel workshop please fill out the form below:
</p>


<form onsubmit="WriteToFile(this)">
Type your first name:
<input type="text" name="FirstName" size="20" />
<br>Type your last name:
<input type="text" name="LastName" size="20" />
<br>
<input type="button" value="submit" />
</form>


</body>
</html>