Jump to content

if field is emtpy the script don't continue????


laPistola

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

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