FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Creating or writing to a file in javascript.

 


Dougie1
How can I create a .txt or write to a .txt file in javascript? The .txt file will be on my site and I just need it to be able to be written to.

Thanks.
LukeakaDanish
You can't.

Use php or some other server side language for that...
Dougie1
Is there any way to send a variable to a php script then?
LukeakaDanish
Dougie1 wrote:
Is there any way to send a variable to a php script then?


yep its very easy

add the variable to the end of the url and "get" it using php (example follows)

url: www.mypage.com/index.php?name=bob

php code to "get" it:
Code:
<?php
if (isset($_GET['name'] {           //check whether "name" is set to anything
    $name = $_GET['name'];      //store it in "$name"
}
?>


then read http://dk.php.net/manual/en/function.fwrite.php which is about the php file module...

(if you cant figure it tell me exactly what you want and i will program it...)
Dougie1
Sorry I meant is there any way to send a variable from the javascript to a php script.
LukeakaDanish
Dougie1 wrote:
Sorry I meant is there any way to send a variable from the javascript to a php script.


Yes, and that was what i answered!Very Happy

Either you "call" a new page with document.location and use the $_GET stuff i showed above, or you could give AJAX a try (if you dont want the page to refresh) - however learning AJAX is something that many people can explain way better than me - so search google for it Wink
Dougie1
That involves you having to post the data though. Is there any way to do it all on page load.

You load the page and the file is written to.
LukeakaDanish
Dougie1 wrote:
That involves you having to post the data though. Is there any way to do it all on page load.

You load the page and the file is written to.


eh...well...yea...what kinda thing are you trying to write?

(it sounds like a VERY SIMPLE task using php...)
varun_dodla
You cannot directly write into a .txt (or any other file) from javascript but you can always do it by using JavaScript in combination with Java Applets.
I was able to succesfully read the data in a file into JavaScript in one of my assignments.The process goes this way:

- Create an applet with public methods that can make a URL connection to the file (because u cant open the file directly) and do the required editing for you.
- These public methods can be called from JavaScript to do the job for you.
- Though this makes a http connection , there is no post involved.

You can view the below links for a better understanding:

http://www.javascripter.net/faq/reading2.htm

http://www.javascripter.net/faq/writinga.htm

Hope this helps.
LukeakaDanish
varun_dodla wrote:
You cannot directly write into a .txt (or any other file) from javascript but you can always do it by using JavaScript in combination with Java Applets.
I was able to succesfully read the data in a file into JavaScript in one of my assignments.The process goes this way:

- Create an applet with public methods that can make a URL connection to the file (because u cant open the file directly) and do the required editing for you.
- These public methods can be called from JavaScript to do the job for you.
- Though this makes a http connection , there is no post involved.

You can view the below links for a better understanding:

http://www.javascripter.net/faq/reading2.htm

http://www.javascripter.net/faq/writinga.htm

Hope this helps.


If you do do this, make sure to validate and security check the data SERVER SIDE or it will be very easy for hackers to insert malicious code into your file!
win2k3
Dougie1 wrote:
How can I create a .txt or write to a .txt file in javascript? The .txt file will be on my site and I just need it to be able to be written to.

Thanks.


You must use FSO ( File System Object ) ActiveX in IE&Windows . if you use activeX in your page the browsers notify the user to confirm activeX runtime on the web page .

search google for FSO+Javascript for sampels .
LukeakaDanish
win2k3 wrote:
You must use FSO ( File System Object ) ActiveX in IE&Windows . if you use activeX in your page the browsers notify the user to confirm activeX runtime on the web page .

search google for FSO+Javascript for sampels .


No, thats not relevant, as he wants to write to a file on the server, not the client...anyway if he wanted to store the data locally, cookies would be a much better method...
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.