...

View Full Version : XML getElementsByTagName Error



Neogodfather
02-24-2009, 10:34 PM
I'm very new to Javascript, PHP, and AJAX (just learned all three within the past two weeks). I am making a page to edit user information which is pulled from a database and put into a temporary cookie by PHP. Then Javascript pulls the info from the cookie and an XML file, to populate the drop down menu bars. I am having trouble in putting the info from the XML files into an array. My code is probably in very bad form.


window.onload = initAll;
var xhr = false;
var selectTextArray = new Array();
var selectValueArray = new Array();
var dataString;
var fieldId = new Array("id", "password_hash", "username", "privileges", "fname", "mname", "lname", "email", "gender", "dob", "address", "city", "state", "zip", "home_phone", "cell_phone");
var nameArray = new Array();
var textArray = new Array();
var valueArray = new Array();

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

if (xhr) {
xhr.onreadystatechange = setXMLArray;
xhr.open("GET", "../select.xml", true);
xhr.send(null);
}
else {
alert("Sorry, but I couldn't create an XMLHttpRequest");
}
for(var i = 0; i < nameArray.length; i++){
alert(nameArray[i]);
selectTextArray[nameArray[i]] = textArray[i];
selectValueArray[nameArray[i]] = valueArray[i];
}
getData();
setField();
}

function setXMLArray() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseXML) {
var selectTag = xhr.responseXML.getElementsByTagName("select");
for(var i = 0; i < selectTag.length; i++){
var allItems = selectTag[i].getElementsByTagName("item");
nameArray[i] = selectTag[i].getElementsByTagName("name");
for (var j = 0; j < allItems.length; j++) {
var XMLTextArray = new Array();
var XMLValueArray = new Array();
XMLTextArray[j] = allItems[j].getElementsByTagName("text");
XMLValueArray[j] = allItems[j].getElementsByTagName("value");
textArray[i][j] = XMLTextArray[j].nodeValue;
textArray[i][j] = formatString(textArray[i][j]);
valueArray[i][j] = XMLValueArray[j].nodeValue;
valueArray[i][j] = formatString(valueArray[i][j]);
}
}
}
}
else {
alert("There was a problem with the request " + xhr.status);
}
}
}


function getData(){
var cookies = document.cookie.split("; ");
for (var i = 0; i < cookies.length; i++){
if(cookies[i].split("=")[0] == "user_info"){
var allData = cookies[i].split("=")[1];
dataString = allData.split("+%3A+");
}
}
}

function setField(){
for(var i = 0; i < dataString.length; i++){
var field = document.getElementById(fieldId[i])
dataString[i] = formatString(dataString[i]);
if(field.type == "select-one"){
selectSet(field, dataString[i]);
}
else{
field.value = dataString[i];
}
}
}

function selectSet(field, data){
for(var i = 0; i < selectTextArray[field.id].length; i++){
var menuItem = document.createElement('option');
menuItem.value = selectArray[field.id][i];
menuItem.text = selectArray[field.id][i];
alert(selectArray[field.id][i] + " " + data);
if(selectArray[field.id][i] == data){
menuItem.selected = true;
}
try{
field.add(menuItem, null);
}
catch(ex){
field.add(menuItem);
}

}
}

function formatString(string){
while(string.indexOf("+") > -1){
string = string.replace("+", " ");
}
while(string.indexOf("%40") > -1){
string = string.replace("%40", "@")
}
while(string.indexOf("%2F") > -1){
string = string.replace("%2F", "/")
}
return string;
}

I put the line I'm having problem with in bold.

The XML file follows the structure of
<choices>
<select>
<name></name>
<item>
<text></text>
<value></value>
</item>
</select>
</choices>

Please somebody help!

Edit:
Nvm I had to use getElementsByTagName("text")[0]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum