Edit XML file with PHP form



I have an XML file that I want to be able to edit using a form.


student.xml



<students>
<student>
<name>Jane Doe</name>
<email>email@email.com</email>
<school>School Name</school>
<coach>Coach Name</coach>
</student>
</students>


Basically my form is set up like this:



<form action= method="post">
<label>School: </label>
<input name="school" type="text" id="school" />
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>


PHP code in head of form:



<?php
$xmlDoc = simplexml_load_file('student.xml')

if(isset($_POST['submit'])){

$s_school=$_POST['school']; //gets data from form

$file = "student.xml";

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

$school=$xmlDoc->getElementsByTagName("school");
$school_value=$xmlDoc->createTextNode($s_school);
$school->appendChild($school_value);

$post = $_POST;
unset($post['submit']);

$doc->save($file);
}

?>


How can I use a form to edit my XML file? I don't want to add a new child I just want to edit the existing nodes.


No comments:

Post a Comment