Jump to content

Not even simple script works...


aximbigfan

Recommended Posts

I have been trying my hardest, but no matter what, nothing I code with AJAX works :(.

 

Here is a very, very simple script just to get the time from a PHP file I have, that echoes the time, and put it in a text box called "timebox". It puts "undefined" in the box instead. I know for a fact fro my servers 404 logs that it is getting all the files it needs, but still.

 

Lets just assume that who ever is accessing the page is using Firefox. I'll change it after I get it to work.

 

At this point, I have tried 4 different forums, and am just feed up with javascript. Can someone please help?

 

<script type='text/javascript'>
function time()
{
var http = new XMLHttpRequest();

http.onreadystatechange=function()
{
if (http.readyState==4)
{
document.time.timebox.value=http.responceText;
}
}
http.open("GET","../libs/time.php", true);
http.send(null);
}
</script>";
<form name='time'><input type='text' name='timebox'><input type='button' value='Server Time' name='timebutton'  onclick="document.location.href='javascript:time();'"></form>

 

Also, why is it that when I have buttons with functions in them, for example onclick='function()', it never works? I always have to put onclick="document.location.href='javascript:function();'"

 

Chris

Link to comment
Share on other sites

Well first of all, you spelled response wrong in responseText.

 

Second, its probably not working if you are testing it on IE, because the method you are using to create the HTTP request object will not work with IE. So no HTTPRequestObject is being created.

 

Third, your ready check is not complete - it should be checking for readystate AND status, not just readystate

 

Fourth, I believe that with get requests you send a value of false, not true. Although I could be wrong - I just leave it out altogether.

 

Fifth, you are using outdated javascript with your code that reads document.time.timebox.value. The DOM object model (which is the current trend in javascript) doesn't access elements by name.

 

I use this as my ajax frame:

 

var XMLHttpRequestObject
try
{
// Firefox, Opera 8.0+, Safari
XMLHttpRequestObject=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
	XMLHttpRequestObject=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
	try
	{
		XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e)
	{
		alert("Your browser does not support AJAX!");
		return false;
	}
}
}

if(XMLHttpRequestObject)
{
	XMLHttpRequestObject.open("GET", URL);

	XMLHttpRequestObject.onreadystatechange = function()
	{
		if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
		{

		}
	}
	XMLHttpRequestObject.send(null);
}

 

Its never not worked yet.

 

For the inner function, I would use this:

 

document.getElementById("target").value = XMLHttpRequestObject.responseText

 

That code requires this HMTL:

 

<input type="text" id="timebox" name="timebox" />

 

Edit: fixed

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.