this is my php code: after execute this php, the data display bottom on this file. i want it to just add in one staff element.
$name = $_POST['sName'];
$ic = $_POST['sIC'];
$email = $_POST['sEmail'];
$address = $_POST['sAddress'];
$nophone = $_POST['sPhone'];
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
//loading the existing XML file
$dom->load('phone_XML.xml');
write new xml node in same file php:
$phone = $dom->documentElement;
$staff = $dom->documentElement;
// appendChild() can be combined with the create*()
//$staff = $phone->appendChild($dom->createElement("staff"));
// you missed the `s` element node
$s = $staff->appendChild($dom->createElement('s'));
$s->setAttribute('id', 'S002Doe');
$s
->appendChild($dom->createElement('sName'))
->appendChild($dom->createTextNode($name));
$s
->appendChild($dom->createElement('sIC'))
->appendChild($dom->createTextNode($ic));
$s
->appendChild($dom->createElement('sEmail'))
->appendChild($dom->createTextNode($email));
$s
->appendChild($dom->createElement('sAddress'))
->appendChild($dom->createTextNode($address));
$s
->appendChild($dom->createElement('sPhone'))
->appendChild($dom->createTextNode($nophone));
save on the same file:
$dom->save('phone_XML.xml');
echo "<script language='JavaScript'>alert('Add item succesful');</script>";
echo "<script language='JavaScript'>window.location = 'index.php';</script>";
?>
output should be like this :
<phone>
<staff>
<s id="S001Akmal">
<sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
<sIC>940228-10-6101</sIC>
<sEmail>muhdnurakmal@velocity.net.my</sEmail>
<sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
<sPhone>012-3456789</sPhone>
</s>
<s id="S002Atikah">
<sName> Nur Atikah Binti Rohizad </sName>
<sIC>940421-14-6236</sIC>
<sEmail>nuratikah@gmail.com</sEmail>
<sAddress>No 24, Taman Saujana Impian, 43000 Kajang</sAddress>
<sPhone>013-6752800 </sPhone>
</s>
</staff>
</phone>