brocky Posted November 8, 2007 Share Posted November 8, 2007 Hi i was wanting to have a div that updates every second or so that displays how long the user has left on the network. I have a basic script that will fetch fro PHP the time the user entered into the zone through MySQL, it all works except for it doesn't update when the data changes. Any ideas anyone? Javascript code (ajax.php) <?php include_once("../../../config.php"); include_once($CFG->dirroot . "/lib/lib.php"); prep_js("ajax.php"); ?> //The core AJAX script for all ajax calls made in this site function createRequestObject() { var req; if(window.XMLHttpRequest) { //Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { //Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { //There is an error creating the object, just as an old browser is being used. alert('Problem creating the XMLHttpRequest object'); } return req; } var http = createRequestObject(); //Make the XMLHttpRequest object //The code for the AJAX calls made by the login form (mainly the Country -> State -> City selector) function sendTimeInQuar(timeVar) { http.open('get', '<?php echo $CFG->wwwroot; ?>/lib/ajax.php?a=time&timeVar=' + timeVar); http.onreadystatechange = handleTimeInQuar; http.send(null); } function handleTimeInQuar() { if(http.readyState == 4 && http.status == 200) { var response = http.responseText; //Text returned FROM the PHP script if(response) { if(document.getElementById("Remaining")) document.getElementById("Remaining").innerHTML = response; else alert("An AJAX error has occured"); } } } PHP code (ajax.php) <?php include_once("../config.php"); include_once($CFG->dirroot . "/lib/lib.php"); if(($_GET['timeVar']+300)-time() > 0) { echo date("i:s", ($_GET['timeVar']+300)-time()); } else { echo "<span style=\"font-weight:bold;color:#FF0000\">Expired</span>"; } ?> PHP code (index.php) <span id="Remaining"></span><script type="text/javascript">sendTimeInQuar('<?php echo select_client("time"); ?>');</script> Quote Link to comment Share on other sites More sharing options...
brocky Posted November 8, 2007 Author Share Posted November 8, 2007 For those wondering i forgot to put a setInterval around the div updater Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.