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.

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

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

How to execute JavaScript functions from Actionscript, then?

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

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.

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:

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?

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.

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!

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.

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

Cool, glad it works.

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

If you need to do a lot of this kind of thing then you should not use serverside javascript. Use some other language. The only "benefit" of servrside JS is the imaginary simplicity of using one language for everything. But this isn't really true, you're also using html, css, .... These are also programming languages. What's one more?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.