Jump to content

[SOLVED] Catch a known error


mwl707

Recommended Posts

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 ;

 

}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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