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 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.
The following worked for me.
Sidenotes: However, am unsure why you're using $data->student->coach=$_POST['coach'];
instead of $data->item->coach=$_POST['coach'];
(Also consult footnotes) - Important.
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('student.xml');
$data->item->name=$_POST['name'];
$data->item->school=$_POST['school'];
$data->item->coach=$_POST['coach'];
$handle=fopen("student.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}
$data=simplexml_load_file('student.xml');
$welcome=$data->item->name;
$school=$data->item->school;
$coach=$data->item->coach;
?>
<?php
echo $welcome . " ";
echo $school . " ";
echo $coach . " ";
?>
<form method="post">
Coach name: <br>
<input type = "text" name = "coach"> Present value in file: <?php echo $coach; ?>
<br><br>
School name: <br>
<input type = "text" name = "school"> Present value in file: <?php echo $school; ?>
<br><br>
Name:
<br>
<textarea name = "name"><?php echo $welcome; ?></textarea>
<br>
<input type="submit" name="submit" value="submit">
</form>
Which produced (after editing it also using different values):
<?xml version="1.0"?>
<welcome>
<item>
<name>name 1</name>
<school>St-Peter</school>
<coach>Robert</coach>
</item>
</welcome>
Editing using different values produced:
<?xml version="1.0"?>
<welcome>
<item>
<name>name 2</name>
<school>St-Andrews</school>
<coach>George</coach>
</item>
</welcome>
Footnotes:
In order for it to work properly is, that an existing file with the following structure must already exist.
<welcome>
<item>
<name>A name</name>
<school>A school name</school>
<coach>A coach's name</coach>
</item>
</welcome>
I have noticed that you are using <form action= method="post">
. It must be changed to:
<form action="" method="post">
Also, if you want to link to a file, it must be like <form action="afile.html" method="post">
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.