xerox_personality Posted November 7, 2006 Share Posted November 7, 2006 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 responseecho $responseText;?>[/code]This is the webpage that runs it [url=http://xeroxtest.awardspace.com/halo_hol/]http://xeroxtest.awardspace.com/halo_hol/[/url] Quote Link to comment https://forums.phpfreaks.com/topic/26407-problem-with-code/ Share on other sites More sharing options...
ober Posted November 7, 2006 Share Posted November 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/26407-problem-with-code/#findComment-120777 Share on other sites More sharing options...
xerox_personality Posted November 7, 2006 Author Share Posted November 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/26407-problem-with-code/#findComment-120793 Share on other sites More sharing options...
xerox_personality Posted November 9, 2006 Author Share Posted November 9, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/26407-problem-with-code/#findComment-122134 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.