Jump to content

Displaying multiple results with array


jnjk

Recommended Posts

I am having problems populating multiple divs with different content. I have an array that contains a url with two different parameters. The html contains two divs with the id's updateArea0 and updateArea1. The result I get is that only updateArea1 is populated with the results of the second array element. Here is the code.

[code]window.onload = initAll;

var xhr = null;
var categories = new Array("ajax.php?cats=engines", "ajax.php?cats=computers");

function initAll() {
        for(var i=0; i<2; i++) {
                getNewFile(categories[i],i);
        }

}

function getNewFile(url,i) {
        makeRequest(url,1);
        return false;
}

function makeRequest(url,i) {
        if(window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
        } else {
                if(window.ActiveXObject) {
                        try {
                                xhr = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch(e) {}
                }
        }

        if(xhr) {
                xhr.onreadystatechange = function() { showContents(i) };
                xhr.open("GET",url,true);
                xhr.send(null);
        }
        else {
                document.getElementById("updateArea").innerHTML = "Sorry but the data is unavailable";
        }
}

function showContents(i) {
        x = i + 7;
        currDiv = "updateArea" + i;
        if(xhr.readyState == 4) {
                if(xhr.status == 200) {
                        var outMsg = (xhr.responseXML && xhr.responseXML.contentType == "text/xml") ?

                xhr.responseXML.getElementsById("choices")[0].textContent : xhr.responseText;
                }
                else {
                        var outMsg = "There was a problem with the request " + xhr.status;
                }
               
                elem = document.getElementById(currDiv);
                elem.innerHTML = outMsg;
               

        }
}
[/code]

Thanks in advance.
Link to comment
https://forums.phpfreaks.com/topic/26274-displaying-multiple-results-with-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.