First ajax page. Will not function

Attempts to send an http request to time.php (which prints the timesta

Page 1 of 1

3 Replies - 1026 Views - Last Post: 20 August 2008 - 05:47 AM

#1 brawnyman713  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 139
  • Joined: 21-October 07

First ajax page. Will not function

Post icon  Posted 19 August 2008 - 08:12 PM

So I don't do a lot of javascript, and I decided I would try ajax. I made an extremely simple time.php script which simply prints the time as a *nix timestamp, and only the timestamp, to the page. It is used to correspond my first script which attempts to send an HTTP request to the page. It is then supposed to paste the contents into the "theTime" text input field when the onkeyup event is triggered in the "username" field. I'm currently doing a tutorial from w3schools, and thats what it was having me do. When the event is triggered, nothing apparent happens. Here is the code for firstapp.html:
<html>
<body>

<script type="text/javascript">

function ajaxFunction()
{
	var xmlHttp;
	// Most major browsers
	try
	{
		xmlHttp=new XMLHttpRequest();
	{
	catch(e)
	{
		// The "special" browser (IE (6.0+)
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{	// The "ultra special short bus" browser (IE (5.5+))
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			// Browser that does not support AJAX
			catch(e)
			{
				alert("Your browser doesn't support AJAX. Too bad.");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.myForm.theTime.value=xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET", "time.php", true);
	xmlHttp.send(null);

}
</script>


<form name="myForm">
Name:	<input type="text" onkeyup="ajaxFunction();" name="username" />
Time:	<input type="text" name="theTime" />
</form>

</body>
</html>



And here is the code for time.php:

<?php
print time();
?>



Any idea what is tripping up and causing the script to not run? It's probably something really simple

Is This A Good Question/Topic? 0
  • +

Replies To: First ajax page. Will not function

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: First ajax page. Will not function

Posted 19 August 2008 - 08:31 PM

Your only problem was tha tyou has a { where you needed a }

<html>
<body>

<script type="text/javascript">

function ajaxFunction()
{
	var xmlHttp;
	// Most major browsers
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		// The "special" browser (IE (6.0+)
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{	// The "ultra special short bus" browser (IE (5.5+))
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			// Browser that does not support AJAX
			catch(e)
			{
				alert("Your browser doesn't support AJAX. Too bad.");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.myForm.theTime.value=xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET", "time.php", true);
	xmlHttp.send(null);

}
</script>


<form name="myForm">
Name:	<input type="text" onkeyup="ajaxFunction();" name="username" />
Time:	<input type="text" name="theTime" />
</form>

</body>
</html>


HTH.
Was This Post Helpful? 1
  • +
  • -

#3 brawnyman713  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 139
  • Joined: 21-October 07

Re: First ajax page. Will not function

Posted 19 August 2008 - 08:37 PM

You know, I spent at least an hour trying to figure out what was wrong, and I was looking for semantic errors. I wouldn't have caught that. Thanks a million
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: First ajax page. Will not function

Posted 20 August 2008 - 05:47 AM

No problem jsut glad to have helped.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1