Jump to content

[SOLVED] AJAX and PHP


brocky

Recommended Posts

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.