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 protected]</email>
<school>School Name</school>
<coach>Coach Name</coach>
</student>
</students>
Basically my form is set up like this:
<form method="post">
<input name="name" id="name" type="text">
<input name="school" id="school" type="text">
<input name="coach" id="coach" type="text">
<br>
<input type="submit" name="submit" value="submit">
</form>
PHP code:
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('student.xml');
$data->student->name=$_POST['name'];
$data->student->school=$_POST['school'];
$data->student->coach=$_POST['coach'];
$handle=fopen("student.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}
$data=simplexml_load_file('student.xml');
?>
How can I use a form to edit multiple nodes in one form? Right now it changes all the nodes to the same as the first one.
Edit: Code is updated. It all seems to be working now. Thanks for the help.
<form action= method="post">should read as<form action="" method="post">if running from inside the same file. If not, do<form action="yourfile.php" method="post">$xmlDoc = simplexml_load_file('student.xml')- Plus, your submit button doesn't hold the same name as your conditional statement.edit, and from there edit that particular key/node on the array, then write it on a file, of course its not good when you have like 10,000 entries.