I have created a two simple ajax calls in an HTML/javascript script. They call on a php routine that just returns a simple string. The string is to be substituted into the lines of HTML text. They work fine, but then the HTML page seems to refresh and wipe out the text that was just inserted there. I have tried it with both the synchronous and asynchronous versions of the xhttp.open. I have my alerts in so that you can test it.
How can I make the text change "stick" so that it is not overwritten. Thanks.
<!DOCTYPE HTML>
<html>
<body>
<h1>AJAX Tester</h1>
<p>Echo here from the server: <span id="text">replace here</span></p>
<form onclick="localform()">
<button type="submit" >Submit</button>
</form>
<p>Echo here from the server: <span id="text1">replace here</span></p>
<form onclick="localform1()">
<button type="submit" >Submit</button>
</form>
<script>
function localform() {
//This routine checks the ajax with a synchronous (call "false" in the open)
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "ajaxtester.php",false);
xhttp.send();
document.getElementById("text").innerHTML = xhttp.responseText;
alert("Test1"+xhttp.responseText) ;
return
}
function localform1() {
//This routine checks the ajax with an asynchronous (call "true" in the open)
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("text1").innerHTML = xhttp.responseText;
alert("Test5"+xhttp.responseText) ;
}
};
xhttp.open("GET", "ajaxtester.php", true);
xhttp.send();
alert("Test4"+xhttp.responseText) ;
return
}
</script>
</body>
</html>
The php code is very simple:
<?php
echo "we made it";
?>
Thank you for the help. Sorry about the mistakes I made in my first two questions, I am a noob here and just learning the ropes.