|
#1
|
|||
|
|||
|
AJAX PHP two div content
I have two calls that work fine independently. When I implement them both on the same page I am getting a "document.getElementById(..) is null or not and object" error. I first call getStateProvince(str) then I call getDistributors(strd). For some reason the script is not passing my second pageElement call.
Here is the code. function callAHAH(url, pageElement, callMessage) { document.getElementById(pageElement).innerHTML = callMessage; try { req=new XMLHttpRequest(); /* e.g. Firefox */ } catch(e) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); /*some versions IE */ } catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); /*some versions IE */ } catch (e) { req = false; } } } req.onreadystatechange = function(){responseAHAH(pageElement);}; req.open("GET",url,true); req.send(null); } function getStateProvince(str){ if (str==""){ alert("Please select a Country."); } else { var strurl="getstate.php"; strurl=strurl+"?id="+str; strurl=strurl+"&sid="+Math.random(); callAHAH(strurl,'displaystateprovince','Please wait; finding your selection..'); } } function getDistributors(strd){ /*alert(str);*/ if (strd==""){ alert("Please select a State/Province"); } else { var strdurl="getdistributors.php"; strdurl=strdurl+"?id="+strd; strdurl=strdurl+"&sid="+Math.random(); callAHAH(strdurl,'displaydistributors','Please wait; finding your distributors..'); } } Thanks for all of you help. |
|
#2
|
|||
|
|||
|
req variable is global
In the callAHAH function, the req variable has global scope because you are missing "var req" declaration within the function scope. Since it is global, the first function creates the req object and sets the onreadystate change and executes the AJAX call asynchronously. The second call also sets req which overrides the previous reference and causing your issue.
To fix just declare your req variable to be local scope. function callAHAH(url, pageElement, callMessage) { var req; .... } |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Php and ajax | sabinkumar | AJAX | 0 | 02-16-2009 05:38 AM |
| Load content in a div by clicking on another div. How? | z1freeride | AJAX | 3 | 01-11-2008 01:41 AM |
| Changing page container div with ajax | wtvamp | AJAX | 2 | 12-14-2007 04:10 PM |
| Using AJAX to fit content in specific area | wallyworth | AJAX | 0 | 05-18-2007 01:31 PM |
| change content of DIV, new content to be a Script | rpesq | Web | 1 | 06-25-2005 02:30 AM |