dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: AJAX PHP two div content

  1. #1
    Join Date
    Oct 2009
    Posts
    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. #2
    Join Date
    Oct 2009
    Posts
    3

    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;
    ....
    }

Similar Threads

  1. Php and ajax
    By sabinkumar in forum AJAX
    Replies: 0
    Last Post: 02-16-2009, 05:38 AM
  2. Replies: 3
    Last Post: 01-11-2008, 01:41 AM
  3. Changing page container div with ajax
    By wtvamp in forum AJAX
    Replies: 2
    Last Post: 12-14-2007, 04:10 PM
  4. Using AJAX to fit content in specific area
    By wallyworth in forum AJAX
    Replies: 0
    Last Post: 05-18-2007, 12:31 PM
  5. Replies: 1
    Last Post: 06-25-2005, 01:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center