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
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
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

 

Sorry just never used onclick with the body really...

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.