bisonsausage
bisonsausage

Reputation: 71

dom cant use variables to store data in xml

I got an error when trying to post user input data from html to php and saved into xml file. It gives me this error Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given. Here is the code

register.htm

<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<form id="regform" method="post" action="register.php">
<fieldset id="person"> 
<legend>Your details:</legend>
<p><label for="fname">First Name:</label><input type="text" name="fname"/></p>
<p><label for="lname">Last Name:</label><input type="text" name="lname"/></p>
<p><label for="password">Password:</label><input type="password" name="password"/></p>
<p><label for="cpassword">Confirm Password:</label><input type="password" name="cpassword"/></p>            
<p><label for="email">Email:</label><input type="email" id="email" name="email"/></p>
<p><label for="phone">Phone:</label><input type="text" name="phone"/></p>
<input type="submit" value="Register"/>
</fieldset>
</form>
</body>
</html>

relevant parts of register.php (whole code is too long)

$fname = @trim($_POST["fname"]);
$lname = @trim($_POST["lname"]);
$password = @trim($_POST["password"]);
$cpassword = @trim($_POST["cpassword"]);
$phone = @trim($_POST["phone"]);
$email = @trim($_POST["email"]);
if(file_exists('../../customer.xml'))
{
    $xml2 = file_get_contents('../../data/customer.xml');
}

if(!file_exists('../../customer.xml'))
        {
            $dir = "../../data";
            $f = fopen("$dir/customer.xml", "w");
            $doc = new DOMDocument('1.0');
            $doc->formatOutput = true;
            $root = $doc->createElement('customer');
            $doc->appendChild($root);
            $user = $doc->createElement('user');
            $root->appendChild($user);
            $fname = $doc->createElement('fname');
            @override - if I change this to 'brian' then it works, doesnt work with $variable
            $fnameValue = $doc->createTextNode($fname);
            $fname->appendChild($fnameValue);
            $user->appendChild($fname);
            $lname = $doc->createElement('lname');
            $lnameValue = $doc->createTextNode($lname);
            $lname->appendChild($lnameValue);
            $user->appendChild($lname);
            echo $doc->save('../../data/customer.xml');
            //$doc->load('customer.xml');
            echo ' Registration Successful!';
        }

Upvotes: 1

Views: 485

Answers (2)

hakre
hakre

Reputation: 198119

You are re-using the $fname variable. That's why you get the error. It's a simple, typographical mistake. One thing you can do is to prefix the input variables with input, e.g. $input_fname so it's clear that you obtain them from there. Or even create a stdclass like $input = new stdClass; and then assign the $_POST variables to it: $input->fname = @trim($_POST["fname"]); - that way it's clear what it stands for and you can pass the input around more easily, too.

Example:

$input = new stdClass;

$input->fname     = @trim($_POST["fname"]);
$input->lname     = @trim($_POST["lname"]);
$input->password  = @trim($_POST["password"]);
$input->cpassword = @trim($_POST["cpassword"]);
$input->phone     = @trim($_POST["phone"]);
$input->email     = @trim($_POST["email"]);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;

$root = $doc->createElement('customer');
$root = $doc->appendChild($root);
foreach ($input as $key => $value) {
    $element = $doc->createElement($key, $value);
    $root->appendChild($element);
}

$doc->save('php://output');

Exemplary output:

<?xml version="1.0"?>
<customer>
  <fname>Waltraud</fname>
  <lname>Van H&#xC3;&#xB6;mpenstetten</lname>
  <password></password>
  <cpassword></cpassword>
  <phone></phone>
  <email></email>
</customer>

Online Demo: https://eval.in/private/29c08d9fafec22

Upvotes: 1

Kevin
Kevin

Reputation: 41873

Just like the error suggests, appendChild needs a DOMNode. Just create that element, then use the second paramter from the user input. Example:

$fname = @trim($_POST["fname"]);
$lname = @trim($_POST["lname"]);
$password = @trim($_POST["password"]);
$cpassword = @trim($_POST["cpassword"]);
$phone = @trim($_POST["phone"]);
$email = @trim($_POST["email"]);

if(file_exists('../../customer.xml')) {
    $xml2 = file_get_contents('../../data/customer.xml');
}

if(!file_exists('../../customer.xml')) {
    $dir = "../../data";
    $f = fopen("$dir/customer.xml", "w");
    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;

    $root = $doc->createElement('customer');
    $doc->appendChild($root);

    $user = $doc->createElement('user');
    $root->appendChild($user);

    $fname_node = $doc->createElement('fname', $fname);
    $user->appendChild($fname_node);

    $lname_node = $doc->createElement('lname', $lname);
    $user->appendChild($lname_node);

    echo $doc->save('../../data/customer.xml');
    //$doc->load('customer.xml');
    echo ' Registration Successful!';
}

Upvotes: 1

Related Questions