aximbigfan Posted March 19, 2008 Share Posted March 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
haku Posted March 19, 2008 Share Posted March 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
aximbigfan Posted March 19, 2008 Author Share Posted March 19, 2008 @ one, yes, I know, I only did that for testing. I'll also look at responseText. Maybe that is why my other script if failing too. I'm a lousy speller . Chris Quote Link to comment Share on other sites More sharing options...
aximbigfan Posted March 19, 2008 Author Share Posted March 19, 2008 YUT!!! After changing the spelling of response it works! Yay! I'll address the other issues tomorrow. Thanks for your help! Chris Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.