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

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.