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
https://forums.phpfreaks.com/topic/96852-not-even-simple-script-works/
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

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.