Reputation: 27
I am attempting to use php to print out a line of text onto a html file, but when I run the html, it merely prints the word undefined. How can I fix this? Here is the code:
HTML:
<html>
<head>
</head>
<body>
<script type="text/javascript">
var xmlhttp=new XMLHttpRequest();
var textinhtml=document.createElement("div");
var text=document.createTextNode(xmlhttp.open("GET","http://mikeyrichards.bugs3.com/test.php",true));
textinhtml.appendChild(text);
document.body.appendChild(textinhtml);
xmlhttp.send();
</script>
</body>
PHP:
<?php
echo "Hello, World"
?>
Upvotes: 1
Views: 162
Reputation: 4750
As commented your usage of XMLHttpRequest
is wrong, you should check online example of it, like this example from the mozilla dev network
Adapted to your script :
<script type="text/javascript">
var xmlhttp=new XMLHttpRequest();
xmlhttp.onload = function() {
var textinhtml=document.createElement("div");
var text=document.createTextNode(this.responseText);
textinhtml.appendChild(text);
document.body.appendChild(textinhtml);
}
xmlhttp.open("GET","http://mikeyrichards.bugs3.com/test.php",true)
xmlhttp.send();
</script>
this script does not account for the errors from the request. Please read the documentation to handle them.
Upvotes: 2