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

Link to comment
Share on other sites

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.

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.