mwl707 Posted September 13, 2009 Share Posted September 13, 2009 I am using this code to extract XML data add2 = address2[position].firstChild.data ; The problem is when is comes back from my php script it may or maynot contain a value (dependent on what info is stored in my database). The problem is when I try to use the variable and it has no value I get an error. Can somebody please tell me how to trap the error and continue. I hope this makes sense If needed here is the full script I have highlighted the error , the code is for a search address box , Thanks // JavaScript search address function searchaddress() { document.getElementById("addressboxsearch").style.visibility = "visible"; if(XMLHttpRequestObject) { searchstring = document.form1.nam.value XMLHttpRequestObject.open("GET", "search_action.php?ss="+searchstring, true); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { var xmlDocument = XMLHttpRequestObject.responseXML; coursename = xmlDocument.getElementsByTagName("sitelist"); address1 = xmlDocument.getElementsByTagName("address1"); address2 = xmlDocument.getElementsByTagName("address2"); postcode = xmlDocument.getElementsByTagName("postcode"); number1 = xmlDocument.getElementsByTagName("number1"); number2 = xmlDocument.getElementsByTagName(" number2"); contactname = xmlDocument.getElementsByTagName("contactname"); coursegroup = xmlDocument.getElementsByTagName("coursegroup"); courseprice = xmlDocument.getElementsByTagName("courseprice"); numres = coursename.length ; var msg = ""; for (loop=0; loop<numres; loop++) { rcoursename = coursename[loop].firstChild.data ; links = "<a href= \"javascript:selected('" + rcoursename + "','" + loop + "'" + ") \" >" + rcoursename + "</a>" ; msg = msg + links ; msg = msg + "<br>" ; } document.getElementById("addressboxsearch").innerHTML = "<b> " + numres + " Results returned </b><br>" + msg ; } } XMLHttpRequestObject.send(null); } } // end of function function selected(name,position) { document.getElementById("addressboxsearch").style.visibility = "hidden"; document.form1.nam.value = name document.form1.add1.value = address1[position].firstChild.data ; /* error here add2 = address2[position].firstChild.data ; if (add2) { document.form1.add2.value = address2[position].firstChild.data } ; */ document.form1.pcode.value = postcode[position].firstChild.data ; document.form1.primarycontactnumber.value = number1[position].firstChild.data ; document.form1.contactname.value = contactname[position].firstChild.data ; } 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.