Greywacke Posted April 24, 2014 Share Posted April 24, 2014 hi all! first let me be clear - the ajax skeletal of the script is not the issue here. I have been working through night to ensure those portions are all functioning properly. i successfully load an xml document with the value of selected for each service, eiter displayed as selected or a blank string. however - when i attempt to select the selected service (in memory) in the dropdown after refreshing the xml, it selects nothing... could an bright sparks perhaps assist me in locating and resolving the issue? below is the relevant javascript portions: // parse services xml (this works perfectly so please do not think it is at fault if "unable to see the forest for the lemons") var services = xmldoc.getElementsByTagName("service"); var arr10 = new Array(); servicearr = new Array(); for (var x = 0; x < services.length; x++) { if (services[x].attributes.getNamedItem("sid") && services[x].attributes.getNamedItem("desc") && services[x].attributes.getNamedItem("selected") && indexOf.call(servicearr,services[x].attributes.getNamedItem("sid").nodeValue)==-1) { var arr = new Array(); var aa = 0; var arr1 = new Array(); arr[0] = services[x].attributes.getNamedItem("sid").nodeValue; arr[1] = services[x].attributes.getNamedItem("desc").nodeValue; arr[2] = services[x].attributes.getNamedItem("selected").nodeValue; var i = 1; for (var c = 0; c < services[x].childNodes.length; c++) { if (services[x].childNodes[c].nodeName == "attribute" && services[x].childNodes[c].attributes.getNamedItem("id").nodeValue != undefined) { var arr2 = new Array(); arr2[0] = services[x].childNodes[c].attributes.getNamedItem("id").nodeValue; arr2[1] = services[x].childNodes[c].attributes.getNamedItem("desc").nodeValue; arr2[2] = services[x].childNodes[c].attributes.getNamedItem("val").nodeValue; arr2[3] = services[x].childNodes[c].attributes.getNamedItem("ord").nodeValue; arr2[4] = (services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue!="")?services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue:""; arr1[aa] = arr2.join(";;;"); aa++; } } arr[3] = arr1.join(";.;"); // service attributes //salert(arr); if (arr.length) { addrecord("menu_services", arr); servicearr.push(arr[0]); svc = true; } } } // single addrecord function function addrecord(id, arr) { salert(id); salert(arr[0]); salert(arr[1]); salert(arr[2]); var opt0 = document.createElement("option"); //salert(arr[i]); opt0.text = arr[1]; // value 1 text service name opt0.value = arr[0] + "|;|" + // value 0 service id arr[2] + "|;|" + // save selection flag ("selected" or "") arr[3]; // save the value 3 attribute keys/values/order array string opt0.selected = (arr[2]=="selected")?true:false; var sel = document.getElementById(id); try { sel.add(opt0, null); // standards compliant; doesn't work in IE } catch(ex) { sel.add(opt0); // IE only } if (arr[2] == "selected") { sid = arr[0]; // save sid if selected! //sel.selectedIndex = sel.options.length-1; } } to prove that the xml loads correctly - here is an excerpt with the Bakky Canopy service... <root> <service sid="10" desc="Accounting Bookkeeping" selected="">...</service> <service sid="1" desc="Bakkie Canopy" selected="selected">...</service> <service sid="3" desc="Bakkie Canopy Accessories" selected=""></service> <service sid="2" desc="Bakkie Linings & Rubberizing" selected=""></service> <service sid="15" desc="Broadband Internet" selected="">...</service> <service sid="5" desc="Business Phone Systems" selected="">...</service> <service sid="9" desc="Company Registrations" selected="">...</service> <service sid="11" desc="Conference Venues" selected="">...</service> <service sid="8" desc="Debt Collection Agencies" selected="">...</service> <service sid="17" desc="Office Coffee" selected="">...</service> <service sid="7" desc="Office Colour Printers" selected="">...</service> <service sid="12" desc="Office Furniture" selected="">...</service> <service sid="13" desc="Office Movers" selected="">...</service> <service sid="14" desc="Office Network Cabling" selected="">...</service> <service sid="6" desc="Office Printers & Copiers" selected="">...</service> <service sid="16" desc="Office Water" selected="">...</service> <service sid="4" desc="Placeholder" selected=""></service> <sql/> </root> as can be seen in the xml and javascript - the service id 1 should load as default selected service id... i am hereby requesting javascripters with great insightto see what i do not - the services all load, but none are selected in the html web form. many thanks in advance to the one (or more) whom with crystal clarity, can resolve this. sincerely - Pierre "Greywacke" du Toit Quote Link to comment https://forums.phpfreaks.com/topic/287989-trying-to-load-default-selected-item-by-top-level-sid-variable-which-is-retrieved-from-xml/ Share on other sites More sharing options...
Greywacke Posted April 24, 2014 Author Share Posted April 24, 2014 in an ongoing attempt to resolve this on my own while i await generous assistance from the other javascripting forum users - the ajax_attributes.js portions get updated continuously, the xml is perfect. selecting the xml and attributes is perfect. handling it in javascript is not... *facepalms* the script has sofar been updated as follows... // top level "global" variables set at top of script var sid = 0; var servicearr = new Array(); // plannned future top level variables for reading and storing the attribute ancestrees var pss = "0"; var ddcnt = 0; var keys = new Array(new Array(),new Array()); // parse services var services = xmldoc.getElementsByTagName("service"); var arr10 = new Array(); servicearr = new Array(); for (var x = 0; x < services.length; x++) { if (services[x].attributes.getNamedItem("sid") && services[x].attributes.getNamedItem("desc") && services[x].attributes.getNamedItem("selected") && indexOf.call(servicearr,services[x].attributes.getNamedItem("sid").nodeValue)==-1) { var arr = new Array(); var aa = 0; var arr1 = new Array(); arr[0] = services[x].attributes.getNamedItem("sid").nodeValue; arr[1] = services[x].attributes.getNamedItem("desc").nodeValue; arr[2] = services[x].attributes.getNamedItem("selected").nodeValue; var i = 1; for (var c = 0; c < services[x].childNodes.length; c++) { if (services[x].childNodes[c].nodeName == "attribute" && services[x].childNodes[c].attributes.getNamedItem("id").nodeValue != undefined) { var arr2 = new Array(); arr2[0] = services[x].childNodes[c].attributes.getNamedItem("id").nodeValue; arr2[1] = services[x].childNodes[c].attributes.getNamedItem("desc").nodeValue; arr2[2] = services[x].childNodes[c].attributes.getNamedItem("val").nodeValue; arr2[3] = services[x].childNodes[c].attributes.getNamedItem("ord").nodeValue; arr2[4] = (services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue!="")?services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue:""; arr1[aa] = arr2.join(";;;"); aa++; } } arr[3] = arr1.join(";.;"); // service attributes //salert(arr); if (arr.length) { addrecord("menu_services", arr); servicearr.push(arr[0]); svc = true; } } } as for the xml - it is still without errors, and is still generated as follows. <root> <service sid="10" desc="Accounting Bookkeeping" selected="">...</service> <service sid="1" desc="Bakkie Canopy" selected="selected">...</service> <service sid="3" desc="Bakkie Canopy Accessories" selected=""></service> <service sid="2" desc="Bakkie Linings & Rubberizing" selected=""></service> <service sid="15" desc="Broadband Internet" selected="">...</service> <service sid="5" desc="Business Phone Systems" selected="">...</service> <service sid="9" desc="Company Registrations" selected="">...</service> <service sid="11" desc="Conference Venues" selected="">...</service> <service sid="8" desc="Debt Collection Agencies" selected="">...</service> <service sid="17" desc="Office Coffee" selected="">...</service> <service sid="7" desc="Office Colour Printers" selected="">...</service> <service sid="12" desc="Office Furniture" selected="">...</service> <service sid="13" desc="Office Movers" selected="">...</service> <service sid="14" desc="Office Network Cabling" selected="">...</service> <service sid="6" desc="Office Printers & Copiers" selected="">...</service> <service sid="16" desc="Office Water" selected="">...</service> <service sid="4" desc="Placeholder" selected=""></service> <sql/> </root> obviously, this abbreviated excerpt comes from google chrome's view of the file, which has the following header info: <?xml version="1.0" encoding="utf-8"?> if nobody wants to help then it's also fine - i am getting there as fast as possible under the current circuimstances - unfortunately it makes for a stressful job where i do not get paid till it is complete sincerely - Pierre "Greywacke" du Toit. Quote Link to comment https://forums.phpfreaks.com/topic/287989-trying-to-load-default-selected-item-by-top-level-sid-variable-which-is-retrieved-from-xml/#findComment-1477201 Share on other sites More sharing options...
Greywacke Posted April 26, 2014 Author Share Posted April 26, 2014 (edited) ok success at last! i've managed to get the scvript working by modifying it as follows: // top level variables var service = -1; var ajaxloading = 0; var sid = 0; // javascript xml reading section var services = xmldoc.getElementsByTagName("service"); var arr10 = new Array(); servicearr = new Array(); for (var x = 0; x < services.length; x++) { if (services[x].attributes.getNamedItem("sid") && services[x].attributes.getNamedItem("desc") && services[x].attributes.getNamedItem("selected") && indexOf.call(servicearr,services[x].attributes.getNamedItem("sid").nodeValue)==-1) { var arr = new Array(); var aa = 0; var arr1 = new Array(); arr[0] = services[x].attributes.getNamedItem("sid").nodeValue; arr[1] = services[x].attributes.getNamedItem("desc").nodeValue; arr[2] = services[x].attributes.getNamedItem("selected").nodeValue; var i = 1; for (var c = 0; c < services[x].childNodes.length; c++) { if (services[x].childNodes[c].nodeName == "attribute" && services[x].childNodes[c].attributes.getNamedItem("id").nodeValue != undefined) { var arr2 = new Array(); arr2[0] = services[x].childNodes[c].attributes.getNamedItem("id").nodeValue; arr2[1] = services[x].childNodes[c].attributes.getNamedItem("desc").nodeValue; arr2[2] = services[x].childNodes[c].attributes.getNamedItem("val").nodeValue; arr2[3] = services[x].childNodes[c].attributes.getNamedItem("ord").nodeValue; arr2[4] = (services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue!="")?services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue:""; arr1[aa] = arr2.join(";;;"); aa++; } } arr[3] = arr1.join(";.;"); // service attributes //salert(arr); if (arr.length) { addrecord("menu_services", arr); if (arr[2]!="") document.getElementById("menu_services").selectedIndex = service; servicearr.push(arr[0]); svc = true; } } } // populating function function addrecord(id, arr) { //salert(id); //salert(arr); var opt0 = document.createElement("option"); opt0.text = arr[1]; // value 1 text service name opt0.value = arr[0] + "|;|" + // value 0 service id arr[2] + "|;|" + // save selection flag ("selected" or "") arr[3]; // save the value 3 attribute keys/values/order array string var sel = document.getElementById(id); // get the select element try { sel.add(opt0, null); // standards compliant; doesn't work in IE } catch(ex) { sel.add(opt0); // IE only } if (arr[2]!="") { sid = parseInt(arr[0]); service = sel.options.length - 1; } } as can be seen, the function sets the service top level variable to the index to be marked as selected. once the dropdown is populated by the options in the xml reader, it sets the selectedIndex if the current options arr[2] value was noticed to != "" sincerely - Pierre "Greywacke" du Toit. Edited April 26, 2014 by Greywacke Quote Link to comment https://forums.phpfreaks.com/topic/287989-trying-to-load-default-selected-item-by-top-level-sid-variable-which-is-retrieved-from-xml/#findComment-1477386 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.