0

Having a little problem with saving form data to xml. I've tried adding new element ($ukey) to xml object, but it gives me an error on line with $ukey variable. $newItem->appendChild($xml->createElement('ukey', $ukey));

// Script by Fred Fletcher, Canada.
$fname = $_POST['name'];
$lname = $_POST['email'];
$location = $_POST['cat'];
$report = $phone;
$description = $_POST['content'];
$ukey = date("dmYHis");


$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('/home/mysite/public_html/file.xml');

$element = $xml->getElementsByTagName('reports')->item(0);

$timestamp = $element->getElementsByTagName('timestamp')->item(0);
$fname = $element->getElementsByTagName('fname')->item(0);
$lname = $element->getElementsByTagName('lname')->item(0);
$location = $element->getElementsByTagName('location')->item(0);
$report = $element->getElementsByTagName('report')->item(0);
$description = $element->getElementsByTagName('description')->item(0);
$ukey = $element->getElementsByTagName('ukey')->item(0);

$newItem = $xml->createElement('reports');

$newItem->appendChild($xml->createElement('timestamp', date("F j, Y, g:i a",time())));;

$newItem->appendChild($xml->createElement('fname', $_POST['name']));
$newItem->appendChild($xml->createElement('lname', $_POST['email']));
$newItem->appendChild($xml->createElement('location', $_POST['cat']));
$newItem->appendChild($xml->createElement('report', $phone));
$newItem->appendChild($xml->createElement('description', $_POST['content']));
$newItem->appendChild($xml->createElement('ukey', $ukey));

$xml->getElementsByTagName('entries')->item(0)->appendChild($newItem);

$xml->save('/home/mysite/public_html/file.xml');

Here is the error

Warning: DOMDocument::createElement() expects parameter 2 to be string, object given in /home/mysite/public_html/wp-content/themes/twentytwelve/myform.php on line 214

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /home/mysite/public_html/wp-includes/template-loader.php on line 47 and defined in /home/mysite/public_html/wp-content/themes/twentytwelve/myform.php on line 214

And xml sample

<?xml version="1.0" encoding="UTF-8"?>
<entries>
  <reports>
   <timestamp>September 19, 2013, 11:45 am</timestamp>
   <fname>John Snider</fname>
   <lname>mailATemail.com</lname>
   <location>156</location>
   <report>08974545153</report>
   <description>jhsdhfsdfgsdtasrgsfgasf</description>
   <ukey>156152</ukey>
  </reports>
</entries>

Do I need to change it to string or what is the problem?

Just to clarify, script was working great before I added $ukey

Thanks

7
  • I can only think that you should check that $ukey = $element->getElementsByTagName('ukey')->item(0); this line is returning something you expect. What is the value of $ukey at this point - perhaps use isset($ukey) before attempting to add to the newItem Commented Sep 19, 2013 at 10:49
  • Well, the value at this point should be date("dmYHis"). I think... Commented Sep 19, 2013 at 11:12
  • Do a print_r() to find out whether it really is that. Commented Sep 19, 2013 at 11:16
  • There is also this error below that warning. Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /home/mysite/public_html/wp-includes/template-loader.php on line 47 and defined in /home/mysite/public_html/wp-content/themes/twentytwelve/myform.php on line 214 Commented Sep 19, 2013 at 11:20
  • "Do I need to change it to string or what is the problem?" - The problem is that you use an object as string. Fix the warning first. Also: "script was working great before I added $ukey" - Just revert the change to get it working again. Then create a test-case from scratch, do the changes in the test-case until it works and then change the original script. Commented Sep 19, 2013 at 11:35

1 Answer 1

2
$ukey = date("dmYHis");

Yes, that should be a nice string value containing a formatted date, fine.

$ukey = $element->getElementsByTagName('ukey')->item(0);

Oops, what are you doing here …?

$newItem->appendChild($xml->createElement('ukey', $ukey));

Now, $ukey is not a string value any more, because of the line quoted before – you messed up with your variable names, dude.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for help. I was just following the logic of this script template I used - not really proficient in php. Could you please tell me what should I change? It would also useful for people who find this thread later on. Thanks again
Just rename one of the variables (of course at the points where you use it later on too), to avoid the collision. (And I doubt this is helpful for later readers, because it’s a combination of very spcific code and too little knowldge of the basics, sorry.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.