0

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){
                DATA = 'D='+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,false);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp.setRequestHeader("Content-length", DATA.length);
                xmlhttp.setRequestHeader("Connection", "close");
                xmlhttp.send(DATA);
            }

            function read(){
                var url = "read.php";
                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" onclick="write(document.getElementById('words').getAttribute('value'));" >Write!</button>
        <div id="gets">

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

Code in write.php:

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

Code in read.php:

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

Here is the test page!

I can't understand why that happens; when you press "Write!", the whole html page just suddenly is "Write somethin!".

Please tell me what I am doing wrong, thanks!!

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ErlendHL
1

Your write function has a name which looks familiar.

Try not to use names of actual JavaScript functions.
What is happening is that you are saying:

onclick="write(document.getElementById('words').getAttribute('value'));" >

Which is doing the same as if you have put:

onclick="document.write(document.getElementById('words').getAttribute('value'));" >

See the problem?

0

Your write function has a name which looks familiar.

Try not to use names of actual JavaScript functions.
What is happening is that you are saying:

onclick="write(document.getElementById('words').getAttribute('value'));" >

Which is doing the same as if you have put:

onclick="document.write(document.getElementById('words').getAttribute('value'));" >

See the problem?

Yeah!! Thanks! :icon_razz:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.