Jump to content

problem with code


xerox_personality

Recommended Posts

I've just started using ajax and have run into a problem. The data from my php never reaches the client. I'm not sure if its even reaching the php. I know the php works because I've tested it independantly. When I test the page everything seems to be working fine but the stateChanged function is never called. Any help on this would be much apreciated. I've been looking over this for days and can't work it out. I know javascript fairly well but I'm a noob to php.

JAVASCRIPT:

[code]
function setXMLHttpRequest()
{
var XMLHttp=null; //IDENTIFIER FOR REQUEST

//determin browser type and set XMLHttp
if (window.XMLHttpRequest) //MOZILLA
{
  XMLHttp = new XMLHttpRequest();
  debug("XMLHttpRequest set for Mozilla");
}
else if (window.ActiveXObject) //IE
{
  XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  debug("XMLHttpRequest set for IE");
}
else //NOT SUPPORTED
{
alert("XMLHttpRequest not suported!");
}
}

function loadPage(page)
{
debug(page);
setXMLHttpRequest(); //SETUP THE REQUEST

var url = "serverside/ajaxhandler.php";
url = url+ "?sn=" +page; //ADD QUERY STRING
debug(url);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.open("GET",url); //OPEN THE REQUEST
xmlHttp.send(null); //SEND THE REQUEST
}


function stateChanged()
{
debug(xmlHttp.readyState);
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
  debug(responseText);
  document.getElementById("Content").innerHTML=xmlHttp.responseText;
}
}

function debug(message)
{
debugging = true; //SET DUBUGGINH

if(debugging)
{
  alert(message);
}
}
[/code]

PHP:

[code]
<?php

//SET THE DATA FOR EACH PAGE
$home="this is da home page";
$lame="lame, very lame";
$test="a simple test page... heh";
$green="GREEN!!! MIMIMIMIMIMI GREEEEEEEEEEEN! GREEN GREEN GREEN!";

//GET THE QUERY STRING FROM URL
$sn=$_GET["sn"];

//MATCH QUERY TO PAGE DATA
$responseText="";

if($sn=="home")
{
$responseText=$home;
}
else if($sn=="lame")
{
$responseText=$lame;
}
else if($sn=="test")
{
$responseText=$test;
}
else if($sn=="green")
{
$responseText=$green;
}
else
{
$responseText="Page not found";
}

//output the response
echo $responseText;

?>
[/code]

This is the webpage that runs it [url=http://xeroxtest.awardspace.com/halo_hol/]http://xeroxtest.awardspace.com/halo_hol/[/url]
Link to comment
Share on other sites

I'm not sure where you learned to use AJAX, but you've got problems everywhere.

1) you need to create the object outside of the function.  Right now your object is only in the scope of the function that creates it. 
2)  debug(responseText); is never going to work because responseText only exists as an attribute of the object.
3) xmlHttp.readyState=="complete" isn't even valid.

You can either go to ajaxfreaks.com and read through a tutorial or two or search this board for posts by me where I've shown a full example of a working app.

Link to comment
Share on other sites

Ok creating the object outside the function makes sense. I dunno what I was thinking :s.
debug(responseText) was just me not thinking properly. Easily fixed and not important to the overall workings.
xmlHttp.readyState=="complete" isn't valid? I read on quite a few tutorials that sometimes readyState is returned as a string, complete being the same as 4, so I included it. But in anycase that doesn't really effect the overall workings either.
I'll fix up the object creation and see how it goes.
Thanks for the help ober.
Link to comment
Share on other sites

I finally got the ajax working. I rewrote the loadPage function to include the content of setXMLHttpRequest but the script still wasnt working. After a few more hours of playing around I realised the main reason the script wasnt making it past xmlHttp.onreadystatechange = stateChanged; was because I had spelt the identifier with capitals  :P (XMLHttp)
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.