Jump to content

code is right, but not working.


severndigital

Recommended Posts

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';

?>

Link to comment
https://forums.phpfreaks.com/topic/65500-code-is-right-but-not-working/
Share on other sites

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); 

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.