954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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?
:icon_question:
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.

ErlendHL
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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

samarudge
Posting Whiz
359 posts since May 2008
Reputation Points: 26
Solved Threads: 31
 

Oh, thanks! I will take a look at that.:icon_biggrin:

How to execute JavaScript functions from Actionscript, then?

ErlendHL
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

The easiest way is just using a standard URL call
In AS3 it can be done with

var url:String = "javascript: your_js_function();";
	var request:URLRequest = new URLRequest(url);
	navigateToURL(request, '_blank');

It's different for AS2 but if you Google it I'm sure you can find it

samarudge
Posting Whiz
359 posts since May 2008
Reputation Points: 26
Solved Threads: 31
 

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.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

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. :icon_cheesygrin:

ErlendHL
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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):

<?php
function readFile($fName) {
    $fHandle = fopen($fName, 'r');
    $DATA = fread($fHandle, filesize($fName));
    fclose($fHandle);
    echo "\"$DATA\"";
}
function writeToFile($fName,$Data) {
    $fHandle = fopen($fName, 'a');
    fwrite($fHandle, $Data);
    fclose($fHandle);
}
?>
<script type="text/javascript">
    var i = 1;
    function doAgain(){
        setTimeOut("doAgain();",1000);
        i++;
        PHP_FUNCTION.writeToFile('test.txt',i);
        document.write(PHP_FUNCTION.readFile('test.txt'));
    }
</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?

ErlendHL
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Sending data to the maaan and back!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
            //var val = document.getElementById("words").getAttribute("value");

            function write(DATA){
                var url = "write.php";
                var XML = "data.txt";
                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.open("POST",url,true);
                xmlhttp.send("D="+DATA+'&F='+XML);
            }

            function read(){
                var url = "read.php";
                var XML = "data.txt"
                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.open("GET",url+"?F="+XLM,false);
                xmlhttp.send(null);
                document.getElementById("gets").innerHTML = xmlhttp.responseText;
            }
        </script>
    </head>
    <body>
        <input type="text" id="words" value="Write somethin!" />
        <button type="button" id="but" value = "Write!" onclick="write(document.getElementById('words').getAttribute('value'));" />
        <div id="gets">

        </div>
    </body>
</html>


Code in write.php:

<?php
$NAME = $_POST['F'];
$HANDLE = fopen($NAME, 'w') or die ('CANT OPEN FILE');
fwrite($HANDLE,$_POST["D"]);
fclose($HANDLE);
?>


Code in read.php:

<?php
$NAME = $_POST['F'];
$HANDLE = fopen($NAME,'r') or die ('CANT OPEN FILE');;
$DATA = fread($HANDLE,filesize($NAME));
fclose($HANDLE);
echo $DATA;
?>


Here is the test page!

Please tell me what I am doing wrong, thanks!

ErlendHL
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

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.

Thanks, but I found out that the real problem is:
write(); is the same as document.write();
:)

ErlendHL
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

Cool, glad it works.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 
The way it would work would be JavaScript creates an XmlHttpRequest object which fires off GET/POST method

In principle XMLHttpRequest supports PUT (which be used upload a .txt file).

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You