943,106 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Feb 7th, 2010
0

Write to and read from .txt file on a server with JavaScript/ActionScript?!

Expand Post »
Hi!
I have now learned how to write to and read from .txt files on my server via php, but is it possible to do this with JavaScript?

Like that JavaScript writes to the .txt file every second without needing the user to refresh the page to write or read .txt file (with php).
2:
Do the same thing with ActionScript 2/3.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ErlendHL is offline Offline
58 posts
since Feb 2010
Feb 7th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
Javascript itself cannot write on the server however AJAX would be a good way to do this, the AJAX tutorials at Tizag.com, they are very good.

As for doing it through AS, you would need to create a javascript function and call it from within AS
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Feb 8th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
Oh, thanks! I will take a look at that.

How to execute JavaScript functions from Actionscript, then?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ErlendHL is offline Offline
58 posts
since Feb 2010
Feb 8th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
The easiest way is just using a standard URL call
In AS3 it can be done with
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var url:String = "javascript: your_js_function();";
  2. var request:URLRequest = new URLRequest(url);
  3. navigateToURL(request, '_blank');
It's different for AS2 but if you Google it I'm sure you can find it
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Feb 8th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
You can use JavaScript to load a txt file via the XmlHttpRequest object (AJAX). You can code this yourself, or use framework like jQuery, which makes the process easily, basically use 1 line of code.

For writing, since the txt file is on the server, you need a backend service, like PHP to manipulate the file. You can still use JavaScript to stream the data to the PHP file, nonetheless.

Let me know if you want code.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Feb 8th, 2010
0

Thanks!

Click to Expand / Collapse  Quote originally posted by samaru ...
You can use JavaScript to load a txt file via the XmlHttpRequest object (AJAX). You can code this yourself, or use framework like jQuery, which makes the process easily, basically use 1 line of code.

For writing, since the txt file is on the server, you need a backend service, like PHP to manipulate the file. You can still use JavaScript to stream the data to the PHP file, nonetheless.

Let me know if you want code.
Thanks for all the good posts, everyone! I am currently reading about AJAX, so I will tell if there are something I wanna know or don't understand.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ErlendHL is offline Offline
58 posts
since Feb 2010
Feb 9th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
Is it really possible to execute php functions wiht JavaScript? If so, then AJAX is for little use, cause writing it in php is much easier, like will this pseudo code work (if i get a code for executing a PHP function in JavaScript, of course):

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. function readFile($fName) {
  3. $fHandle = fopen($fName, 'r');
  4. $DATA = fread($fHandle, filesize($fName));
  5. fclose($fHandle);
  6. echo "\"$DATA\"";
  7. }
  8. function writeToFile($fName,$Data) {
  9. $fHandle = fopen($fName, 'a');
  10. fwrite($fHandle, $Data);
  11. fclose($fHandle);
  12. }
  13. ?>
  14. <script type="text/javascript">
  15. var i = 1;
  16. function doAgain(){
  17. setTimeOut("doAgain();",1000);
  18. i++;
  19. PHP_FUNCTION.writeToFile('test.txt',i);
  20. document.write(PHP_FUNCTION.readFile('test.txt'));
  21. }
  22. </script>

This should maybe read the text-file, and write a new (higer) number each second, if you could tell me how to execute php functions from JavaScript.
Is it eventually possible to write to a text file using AJAX?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ErlendHL is offline Offline
58 posts
since Feb 2010
Feb 9th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
The way it would work would be JavaScript creates an XmlHttpRequest object which fires off GET/POST method to a PHP page that has your PHP function. You could also pass variables from JavaScript to your PHP page. I have a meeting right now, so I'll write you some sample code later.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Feb 10th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
I have read about AJAX for a while now, and tryed to use GET and POST, but with not so good results.

Code in index.html:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Sending data to the maaan and back!</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <script type="text/javascript">
  7. //var val = document.getElementById("words").getAttribute("value");
  8.  
  9. function write(DATA){
  10. var url = "write.php";
  11. var XML = "data.txt";
  12. if (window.XMLHttpRequest)
  13. {// code for IE7+, Firefox, Chrome, Opera, Safari
  14. xmlhttp = new XMLHttpRequest();
  15. }
  16. else
  17. {// code for IE6, IE5
  18. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  19. }
  20. xmlhttp.open("POST",url,true);
  21. xmlhttp.send("D="+DATA+'&F='+XML);
  22. }
  23.  
  24. function read(){
  25. var url = "read.php";
  26. var XML = "data.txt"
  27. if (window.XMLHttpRequest)
  28. {// code for IE7+, Firefox, Chrome, Opera, Safari
  29. xmlhttp = new XMLHttpRequest();
  30. }
  31. else
  32. {// code for IE6, IE5
  33. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  34. }
  35. xmlhttp.open("GET",url+"?F="+XLM,false);
  36. xmlhttp.send(null);
  37. document.getElementById("gets").innerHTML = xmlhttp.responseText;
  38. }
  39. </script>
  40. </head>
  41. <body>
  42. <input type="text" id="words" value="Write somethin!" />
  43. <button type="button" id="but" value = "Write!" onclick="write(document.getElementById('words').getAttribute('value'));" />
  44. <div id="gets">
  45.  
  46. </div>
  47. </body>
  48. </html>

Code in write.php:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. $NAME = $_POST['F'];
  3. $HANDLE = fopen($NAME, 'w') or die ('CANT OPEN FILE');
  4. fwrite($HANDLE,$_POST["D"]);
  5. fclose($HANDLE);
  6. ?>

Code in read.php:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. $NAME = $_POST['F'];
  3. $HANDLE = fopen($NAME,'r') or die ('CANT OPEN FILE');;
  4. $DATA = fread($HANDLE,filesize($NAME));
  5. fclose($HANDLE);
  6. echo $DATA;
  7. ?>

Here is the test page!

Please tell me what I am doing wrong, thanks!
Last edited by ErlendHL; Feb 10th, 2010 at 11:28 am.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ErlendHL is offline Offline
58 posts
since Feb 2010
Feb 12th, 2010
0
Re: Write to and read from .txt file on a server with JavaScript/ActionScript?!
From a quick eyeballing...

In index.html:

Lines 12-19 and 27-34 are repetitive. I would remove them from both functions and put that block of code once above line 9, since you're declaring them global anyways.

Line 26 needs a semicolon.

Line 35, you meant XML not XLM. Typo.

Line 43, did you mean to put a closing button tag? It's one of those funky tags where you need a closing one to render correctly.
Last edited by samaru; Feb 12th, 2010 at 12:56 am.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: AJAX, Browsers and HTTP Server Responses
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Calculating Sum and sort of dynamic rows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC