Good Morning

I have an error that I have not been able to resolve.
Fatal error: Call to a member function appendChild() on null in /home/rgoad/map.checotahpolice.org/testfolder/index2.php on line 31

Line 31 [php]$crime->appendChild($emp);[/php]

Thank you in advance

[php]
$xml=new DomDocument(“1.0”,“UTF-8”);
$xml->formatOutput=true;
$xml->preserveWhiteSpace=false;
$xml->load(“cmap.xml”);

if(!$xml)

{
$crime=$xml->createElement(“crime”);
$xml->appendChild($crime);
}
else
{

$crime=$xml->firstchild;    

}
if(isset($_POST[‘submit’]))
{
$fcrime=$_POST[‘a’];
$fdate=$_POST[‘b’];
$flat=$_POST[‘c’];
$flong=$_POST[‘d’];

$emp=$xml->createElement(“crime”);
$crime->appendChild($emp);
$a=$xml->createElement(“a”,“fcrime”);
$emp->appendChild($a);
$b=$xml->createElement(“b”,“fdATE”);
$emp->appendChild($b);
$c=$xml->createElement(“c”,“flat”);
$emp->appendChild($c);
$d=$xml->createElement(“d”,“flong”);
$emp->appendChild($d);
echo"".$xml->saveXML()."";
$xml->save(“cmap.xml”);
}
?>

[/php]

This literally says,

[php] $xml->load(“cmap.xml”);

  if(!$xml)

{
$crime=$xml->createElement(“crime”);
$xml->appendChild($crime);
}[/php]
If xml fails to load, create an element in it.

Im new at this I am not sure what you are wanting. That is an exact copy of what i have written… I get the error when I submit the form that is part of the web page

[php]

Crime Map XML Data <?php $xml=new DomDocument("1.0","UTF-8"); $xml->formatOutput=true; $xml->preserveWhiteSpace=false; $xml->load("cmap.xml");
if(!$xml)

{
$crime=$xml->createElement(“crime”);
$xml->appendChild($crime);
}
else
{

$crime=$xml->firstchild;    

}
if(isset($_POST[‘submit’]))
{
$fcrime=$_POST[‘a’];
$fdate=$_POST[‘b’];
$flat=$_POST[‘c’];
$flong=$_POST[‘d’];

$emp=$xml->createElement(“crime”);
$crime->appendChild($emp);
$a=$xml->createElement(“a”,“fcrime”);
$emp->appendChild($a);
$b=$xml->createElement(“b”,“fdATE”);
$emp->appendChild($b);
$c=$xml->createElement(“c”,“flat”);
$emp->appendChild($c);
$d=$xml->createElement(“d”,“flong”);
$emp->appendChild($d);
echo"".$xml->saveXML()."";
$xml->save(“cmap.xml”);
}
?>

<form action method="POST" action="index2.php">
<p>Crime Type</p><input type="text" name="a" placeholder="Crime Type"/>
  <p>Date</p><input type="text" name="b" placeholder="dd/mm/year"/>
  <p>Latitude</p><input type="shortinteger" name="c" placeholder="Latitude"/>
  <p>Longitude</p><input type="shortinteger" name="d" placeholder="Longitude"/>
<input type="submit"name="submit"value="Submit"/>
[/php]

Thanks

This is the xml

[php]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> Crime Type XX/XX/XXXX 35.469509 -95.522061 [/php]

Correcting your XML markup

[code]<?xml version="1.0" encoding="UTF-8" ?>

Crime Type 09/20/2016 35.469509 -95.522061 [/code]

[php]<?php
$xml = new DomDocument(“1.0”, “UTF-8”);
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load(“cmap.xml”);

if (! $xml) {
$crime = $xml->createElement(“crime”);
$xml->appendChild($crime);
} else {
$crime = $xml->firstchild; // this is what is triggered.
}[/php]

Thank you for your help. I made the adjustments and ether i goofed which is possible or I missed something

this error appears when page loads
Warning: DOMDocument::load(): XML declaration allowed only at the start of the document in /home/rgoad/map.checotahpolice.org/testfolder/cmap.xml, line: 1 in /home/rgoad/map.checotahpolice.org/testfolder/index2.php on line 10

This error appears when data is submitted
Fatal error: Call to a member function appendChild() on null in /home/rgoad/map.checotahpolice.org/testfolder/index2.php on line 31

This is the URL for the page
http://map.checotahpolice.org/testfolder/index2.php

My suggestion:

<?xml version="1.0" encoding="UTF-8"?> <crimes> </crimes>

[php]<?php

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
try {
$xml = new DomDocument(“1.0”, “UTF-8”);
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load(“cmap.xml”);

    $node = $xml->createDocumentFragment();
    $node->appendXML("<crime><fcrime>{$_POST['crime']}</fcrime><fdate>{$_POST['date']}</fdate><flat>{$_POST['lat']}</flat><flong>{$_POST['long']}</flong></crime>");
    $xml->documentElement->appendChild($node);

    $xml->save("cmap.xml");
} catch ( Exception $e ){
    $error = "Error saving data.";
}

}
?>

Crime Map XML Data <?php echo isset( $error ) ? $error : ''; ?>

Crime Type

Date

Latitude

Longitude

[/php]

Thanks astonecipher

It appears I still have issues I copy and pasted exactly what you have here. Thoughts ?
http://map.checotahpolice.org/testfolder/index3.php

Warning: DOMDocument::load(): XML declaration allowed only at the start of the document in /home/rgoad/map.checotahpolice.org/testfolder/cmap.xml, line: 1 in /home/rgoad/map.checotahpolice.org/testfolder/index3.php on line 8

Fatal error: Call to a member function appendChild() on null in /home/rgoad/map.checotahpolice.org/testfolder/index3.php on line 12

Post your version of the xml file

[php]

<?xml version="1.0" encoding="UTF-8"?>

[/php]

Ok got it working. THANK YOU! Had to remove the <?xml version=“1.0” encoding =“UTF-8” line to get things rolling . Thanks again now I need to study more and understand it all

Sponsor our Newsletter | Privacy Policy | Terms of Service