severndigital Posted August 17, 2007 Share Posted August 17, 2007 this is killing me. the code has no errors at least that I can find and the browser doesn't kick out with errors, but it still doesn't run. here is the code <html> <head> <title>Ajax & PHP at work</title> <script language="javascript"> var XMLHttpRequestObject = false; if (window.XMLHttpRequest){ XMLHttpRequestObject = new XMLHttpRequest(); //creates a new request if firefox .. etc } else if (window.ActiveXObject){ XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); //creates a new request if Explorer }// close If statement function getData(dataSource,spanId){ if(XMLHttpRequestObject){ var obj = document.getElementById(spanId); //this is the interaction below XMLHttpRequestObject.open("GET",dataSource); //this will inject the results when the object has completed it's task XMLHttpRequestObject.onReadyStateChange = function(){ //make sure the state is correct by looking for a 4 and also a 200 if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){ obj.innerHTML = XMLHttpRequestObject.responseText; } } //send a null object because we used the GET method for retrieval. XMLHttpRequestObject.send(null); } } </script> </head> <body> <h1>Fetching data with Ajax & PHP</h1> <br> <form> <input type="button" value="Display Message" onClick="getData('data.php','targetSpan')"> </form> <span id="targetSpan">This is where the fetched data will appear.</span> </body> </html> is it me, why isn't this code working?? this is a simple example but it doesn't work. Is there something on my server that needs to be configed or anything like that?? thanks, chris *EDIT data.php does exist. it contain the code below <?php echo 'This text was fetched with Ajax'; ?> Quote Link to comment Share on other sites More sharing options...
mainewoods Posted August 17, 2007 Share Posted August 17, 2007 if you are not getting script errors, but the script doesn't do what it's supposed to, then use alert's to test that the script is getting where it should be and that the value of the variables are as expected at that point: <script language="javascript"> var XMLHttpRequestObject = false; if (window.XMLHttpRequest){ XMLHttpRequestObject = new XMLHttpRequest(); //creates a new request if firefox .. etc } else if (window.ActiveXObject){ XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); //creates a new request if Explorer }// close If statement alert('made it here! ajaxobj=' + XMLHttpRequestObject); 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.