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!!