Jump to content

Beginners Ajax


chief17

Recommended Posts

Hey guys,

 

Im just starting out with AJAX and i am a little stuck already, please could someone point me in the right direction.

 

Basically nothing is happening, the time is supposed to appear/update when you click anywhere on the page.

 

HTML Page:

<html>
<head>
<script type="text/javascript">
var xmlhttp;
//---------------------------------------------------------------------------------------------------------\\
function showTime()
{
xmlhttp = GetXmlHttpObject();

if (xmlhttp == null)
{
	alert ("Your browser does not support XMLHTTP!");
	return;
}

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET","time.php",true);
xmlhttp.send(null);
}
//---------------------------------------------------------------------------------------------------------\\

//---------------------------------------------------------------------------------------------------------\\
function stateChanged()
{
if (xmlhttp.readyState == 4)
{
	document.getElementById("timeBox").innerHTML = xmlhttp.responseText;
}
}
//---------------------------------------------------------------------------------------------------------\\

//---------------------------------------------------------------------------------------------------------\\
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	return new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
	// code for IE6, IE5
	return new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
	return null;
}
}
//---------------------------------------------------------------------------------------------------------\\
}
</script>
</head>
<body onClick="showTime()">
<div id="timeBox">Click anywhere for the time.</div>
</body>
</html>

 

PHP code (time.php):

<?php

echo date("g:i.sa");

?>

 

Any help is much appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/168615-beginners-ajax/
Share on other sites

Hi,

 

Thanks for the reply, onClick with the body tag does actually work so unfortunately that isnt the problem, i however still did try tour tip and sadly it still doesnt work, do you have any other ideas?

 

Thanks

you got error in javascript, you got extra "}" in your javascript just above the </script> tag

Link to comment
https://forums.phpfreaks.com/topic/168615-beginners-ajax/#findComment-889497
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.