I'm really new to the whole JavaScript and AJAX thing. For my final exam, I'm trying to create a web-application. One script sends data from the user to the server and saves it into a textfile
, while another is always presenting the current textfile
to the User.
I got a far as getting the current content of the file displayed on the users GUI for this I'm using this ajax
function:
var xmlHttp = createXmlHttpRequestObject();
//erstellen des Objektes, welches nachher mit der anderen Seite kommuniziert
function createXmlHttpRequestObject(){
var xmlHttp;
if (window.ActiveXObject) {
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if (!xmlHttp) {
alert("cant create that object");
}
else
return xmlHttp;
}
//jede sekunde wird der inhalt von send_note geladen
setInterval(function note(){
if (xmlHttp.readyState==0 || xmlHttp.readyState==4) {
xmlHttp.open("POST", "send_note.php?", true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}
}, 500);
function handleServerResponse(){
if (xmlHttp.readyState==4) {
if (xmlHttp.status==200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("display").innerHTML = message;
setTimeout('note()', 1000);
}else{
alert('something went wrong');
}
}
}
note()
is called when the body of the GUI that the user is presented is loaded.
2 things i cant get to work now:
- How do i send data to the file that responds using the post variable that i have used in this ajax request? And how can i get to this data that i sent to the responding php file in this php file?
- The development tool of google chrome shows me this error: Uncaught ReferenceError: note is not defined
The passage where i call note()
from looks like this:
<body onload="note()">
Can anybody help me with that?