JS error when XML field empty

They have: 222 posts

Joined: Sep 1999

I've got a php function that returns an XML page which is then handled by a JS function, but I get this error if any of the XML fields are empty:

Quote:
Error: xmlDocument.getElementsByTagName("address2Title").item(0).firstChild has no properties
Source File: http://localhost/contactManager/
Line: 47

Here's the JS function:

function showContactInfo2()
{
if (http.readyState == 4)
{
if (http.responseText.indexOf('invalid') == -1)
{
xmlDocument = http.responseXML;

document.getElementById("contactInfo").innerHTML =
xmlDocument.getElementsByTagName('firstName').item(0).firstChild.data +
xmlDocument.getElementsByTagName('lastName').item(0).firstChild.data +

xmlDocument.getElementsByTagName('address1Title').item(0).firstChild.data +
xmlDocument.getElementsByTagName('address1').item(0).firstChild.data +
xmlDocument.getElementsByTagName('city1').item(0).firstChild.data +
xmlDocument.getElementsByTagName('state1').item(0).firstChild.data +
xmlDocument.getElementsByTagName('zipCode1').item(0).firstChild.data +

xmlDocument.getElementsByTagName('address2Title').item(0).firstChild.data +
xmlDocument.getElementsByTagName('address2').item(0).firstChild.data +
xmlDocument.getElementsByTagName('city2').item(0).firstChild.data +
xmlDocument.getElementsByTagName('state2').item(0).firstChild.data +
xmlDocument.getElementsByTagName('zipCode2').item(0).firstChild.data +

xmlDocument.getElementsByTagName('homePhone').item(0).firstChild.data +
xmlDocument.getElementsByTagName('cellPhone').item(0).firstChild.data +
xmlDocument.getElementsByTagName('workPhone').item(0).firstChild.data +

xmlDocument.getElementsByTagName('birthday').item(0).firstChild.data
;
}

else
{
document.getElementById("contactInfo").innerHTML = "Invalid Request";
}
}
}
'

Is there a way to check if a particluar field is empty before trying to use it?

They have: 5,633 posts

Joined: Jan 1970

Usually when I get the "has no properties" error there is a proublem in the code and is tring to open a element that doesnt exist.

They have: 222 posts

Joined: Sep 1999

It exists, here's the script that's returning the data that the JS functions use:

<?php
if(array_key_exists('id', $_GET))
   
$id = $_GET['id'];

if(isset(
$id))
{
   
$dblink = new mysqli('localhost', 'root', 'password', 'contactmanager');
       
$result = $dblink->query(\"SELECT * FROM contacts WHERE id=$id\");
       
       
$row = $result->fetch_array(MYSQLI_ASSOC);
           
$firstName = $row['firstName'];
           
$lastName = $row['lastName'];
           
           
$address1Title = $row['address1Title'];
           
$address1 = $row['address1'];
           
$city1 = $row['city1'];
           
$state1 = $row['state1'];
           
$zipCode1 = $row['zipCode1'];
           
           
$address2Title = $row['address2Title'];
           
$address2 = $row['address2'];
           
$city2 = $row['city2'];
           
$state2 = $row['state2'];
           
$zipCode2 = $row['zipCode2'];
           
           
$homePhone = $row['homePhone'];
           
$cellPhone = $row['cellPhone'];
           
$workPhone = $row['workPhone'];
           
           
$birthday = $row['birthday'];
       
$result->close();
       
   
$dblink->close();
   
   
$output = 'xml version=\"1.0\" standalone=\"yes\"
                <contact>
                    <id>'.
$id.'</id>
                    <firstName>'.
$firstName.'</firstName>
                    <lastName>'.
$lastName.'</lastName>

                    <address1Title>'.
$address1Title.'</address1Title>
                    <address1>'.
$address1.'</address1>
                    <city1>'.
$city1.'</city1>
                    <state1>'.
$state1.'</state1>
                    <zipCode1>'.
$zipCode1.'</zipCode1>
                   
                    <address2Title>'.
$address2Title.'</address2Title>
                    <address2>'.
$address2.'</address2>
                    <city2>'.
$city2.'</city2>
                    <state2>'.
$state2.'</state2>
                    <zipCode2>'.
$zipCode2.'</zipCode2>
                   
                    <homePhone>'.
$homePhone.'</homePhone>
                    <cellPhone>'.
$cellPhone.'</cellPhone>
                    <workPhone>'.
$workPhone.'</workPhone>
                   
                    <birthday>'.
$birthday.'</birthday>
                </contact>';
}

else
{
   
$output = 'xml version=\"1.0\" standalone=\"yes\"
                <contact>
                    <id></id>
                    <firstName>Invalid request</firstName>
                    <lastName></lastName>

                    <address1Title></address1Title>
                    <address1></address1>
                    <city1></city1>
                    <state1></state1>
                    <zipCode1></zipCode1>
                   
                    <address2Title></address2Title>
                    <address2></address2>
                    <city2></city2>
                    <state2></state2>
                    <zipCode2></zipCode2>
                   
                    <homePhone></homePhone>
                    <cellPhone></cellPhone>
                    <workPhone></workPhone>
                   
                    <birthday></birthday>
                </contact>';
}

header('Content-Type: text/xml');
echo
$output;
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.