Please try to adhere as close as possible to my first example ... and if it doesn't work, please come back with your rewritten code. Then we will find the problem.
The goal of this forum is not to write the code for you but to guide you to help yourself as good as possible.
Code:
var myvar = "";
var http = XMLHttpRequest();
http.open('GET', 'datatextfile.txt', true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200)
myvar = http.responseText;
}
http.send(null);
Hint: The first A in Ajax means "asynchronous". I think coming from C++ you should be aware of this construct. Essentially it means that the normal Javascript program flow continues while "some background task" is still running. As soon as the background task triggers some event (in this case the change of the readyState), it will call a so-called "callback function" (onreadystatechange). readyState==4 says: Request complete, status==200 says HTTP OK. In that case you can grab the PHP script output from responseText and do with it whatever you want