0

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.

8
  • 1
    Sidenote: This <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"> Commented Aug 3, 2014 at 3:17
  • 1
    Sidenote #2: You also have a missing semi-colon in $xmlDoc = simplexml_load_file('student.xml') - Plus, your submit button doesn't hold the same name as your conditional statement. Commented Aug 3, 2014 at 3:22
  • This answer stackoverflow.com/a/12514292 might be of help. Commented Aug 3, 2014 at 4:04
  • deleted my answer, not useful anyway, for editing, what i would do is convert the xml into an array, present a CRUD like table, the usual stuff, (the unique identifier is the array key of student node) in the table there is a link called 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. Commented Aug 3, 2014 at 4:07
  • 1
    @Fred-ii- That did it! I matched my file structure and was able to edit the file. Thanks. I saw that question but when I first looked it didn't appear to be what I was looking for. Commented Aug 3, 2014 at 4:13

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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">.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.