laPistola Posted April 12, 2010 Share Posted April 12, 2010 Hello The AJAX call is to a PHP script that returns an XML of the contacts details from the DB (name, company, address etc) This then inturn displays the contacts details on the page. However as i don't need all the details i have an array with only the xml tag names the page uses. here is the script function doContact() { if (xhr.readyState == 4) { if (xhr.status == 200) { if (xhr.responseXML) { // build address details field var addArr = Array("company","name","address1","address2","town","postcode","country"); var addCode = ""; var tags = xhr.responseXML.getElementsByTagName('contact'); for ( var i=0; i < addArr.length; i++) { var val = getVal(tags[0],addArr[i]); if (val!="") { addCode += "<input name=\""+addArr[i]+"\" type=\"text\" class=\"invoiceField\" id=\""+addArr[i]+"\" maxlength=\"255\" style=\"width:95%\" onfocus=\"changeC(this,'fieldAddFocus');changeH(this,'in');\" onblur=\"changeC(this,'invoiceField');changeH(this,'out');\" value=\""+val+"\" />"; if (i<6) { addCode += "<br />\n"; } } } document.getElementById('address_details').innerHTML = addCode; } } else { alert("AJAX Failed at doContactfunction code " + xhr.status); } } function getVal(theData,theTag) { return theData.getElementsByTagName(theTag)[0].firstChild.nodeValue; } } Now it works fine if all the details have been filled in for the contact, but if say the company name is missing or even address2 it will not display anything at all. Thank you Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 I'm guessing there is an error. What does your PHP file return if a field is empty? Does it return nothing? Quote Link to comment Share on other sites More sharing options...
laPistola Posted April 12, 2010 Author Share Posted April 12, 2010 Funny you should say i was just coming back to click solved. The XML output on empty fields from the DB was just <fieldname/> not <fieldname></fieldname> which i knew but forgot (been at this project for hours), the thing that was throwing me was the FF FireBug console wasn't showing errors so i have just changed the PHP script to insert NULL between XML tags if field is empty or NULL then changed the JS to if (val!=NULL) { and its working perfectly Thank you for reply anyway. Quote Link to comment 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.