Reputation: 25
I have an ajax code that calls a php file:
function ajaxRequest(){
var requestData;
try{
// Opera 8.0+, Firefox, Safari
requestData = new XMLHttpRequest();
} catch (e){
// IE
try{
requestData = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
requestData = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Your browser is brokeded.
alert("Please try again later!");
return false;
}
}
}
// Send/receive data
requestData.onreadystatechange = function(){
if(requestData.readyState == 4){
document.getElementById("testDiv").innerHTML = requestData.responseText;
}
}
requestData.open("GET", "tester.php", true);
requestData.send(null); }
What I actually want the code to do is to call a php function on the same page at time intervals. I intend to display a notification with it on adding a new row to a table in a mysql database. Please, help me edit this code or probably a new one if the above is not okay. I will appreciate the help. Thank you
Upvotes: 0
Views: 5642
Reputation: 628
I would suggest you to use jQuery, which is a js framework that offers several functions. An example of your code would be this:
function callMe() { $.ajax({ type: "GET", url: "test.php", data: "id=1", success: function(response){ $("#testDiv").html(response); } }); } // Call it setInterval(callMe, 5000); //every 5 secs
Upvotes: 3
Reputation: 270
<script>
function callMe()
{
$.ajax({
type: "GET",
url: "count.php",
data: "id=1",
success: function(response){
$("#Count").html(response);
}
});
}
// Call it
// "setInterval" means every 5 secs
// "setTimeout" just once...
setInterval(callMe, 5000);
</script>
<p>Count:<span id="Count"></span></p>
Upvotes: 1