I use the following function to make an ajax call to a PHP that retrieves data from a MySQL database whenever an onChange
-Event occurs. The result of the PHP is a JavaScript function including some data.
Basically, I copied all the code down to xmlhttp.send()
from W3Schools.
Then, facing the problem of calling the JavaScript on my page, I added the part with $.getScript(...)
.
It works, but I have a feeling that calling getRun.php?run=...
twice cannot be the best way.
function getRun(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getRun.php?run="+str,true);
xmlhttp.send();
$.getScript("getRun.php?run="+str, function() {
setTimeout();
});
}
}
Do you have any hints on what would be a better way?