title: Saving form data to an existing XML-file using PHP
body: <p>Okay, so here's what I want to do (NOTE: I'm stil new to PHP):</p>
  
  <p>I have a registration form for movies/series etc. The data from the filled form is then registered (register.php) and sent to a MySQL database, which is working fine. Here comes the but: I also want, in the same register file (register.php), the data to be stored in an existing XML-file (data.xml). Important here is that every successfully submitted form is stored in the same XML-file (data.xml).</p>
  
  <p>I display all registered "movies" in a HTML-table through a while-loop, which now collects the data from my database. I'm also begging for help to, somewhere outside the table, add a button which generate/display the content of the XML-file in a new tab(?). Not right-clicking to view source, since the table data is collected from MySQL this is impossible.</p>
  
  <p>Here's what I have so far (this manages to save each submission in data.xml but replaces if another submission is made - I want to add NOT replace):</p>
  
  <p>First off, index.php:</p>
  
  <pre><code>&lt;form enctype="multipart/form-data" action="core/register.php" method="post" autocomplete="true"&gt;
      &lt;p&gt;&lt;input type="text" name="name" placeholder="Program name" /&gt;&lt;/p&gt;
      &lt;p&gt;&lt;input type="date" name="date" placeholder="Program date" /&gt;&lt;/p&gt;
      &lt;p&gt;&lt;input type="time" name="time" placeholder="Program time" /&gt;&lt;/p&gt;
      &lt;p&gt;&lt;input type="text" name="bline" placeholder="B-line" /&gt;&lt;/textarea&gt;&lt;/p&gt;
      &lt;p&gt;&lt;textarea name="synopsis" placeholder="Program synopsis" /&gt;&lt;/textarea&gt;&lt;/p&gt;
      &lt;p&gt;&lt;textarea name="leadtext" placeholder="Lead text" /&gt;&lt;/textarea&gt;&lt;/p&gt;
      &lt;p&gt;&lt;input type="url" name="url" placeholder="URL" /&gt;&lt;/p&gt;
      &lt;p&gt;&lt;input type="submit" value="Register" name="register" /&gt;&lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Next up, data.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;programs&gt;
      &lt;program&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;date&gt;&lt;/date&gt;
          &lt;start_time&gt;&lt;/start_time&gt;
          &lt;b-line&gt;&lt;/b-line&gt;
          &lt;synopsis&gt;&lt;/synopsis&gt;
          &lt;leadtext&gt;&lt;/leadtext&gt;
          &lt;url&gt;&lt;/url&gt;
      &lt;/program&gt;
  &lt;/programs&gt;
  </code></pre>
  
  <p>Finally, register.php:</p>
  
  <pre><code>require_once('db.php');
  
  $str = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;?xml-stylesheet type="text/xsl" href="xsl.xsl"?&gt;&lt;programs&gt;&lt;/programs&gt;';
  $xml = simplexml_load_string($str);
  
  $name = $_POST['name'];
  $date = $_POST['date'];
  $time = $_POST['time'];
  $bline = $_POST['bline'];
  $synopsis = $_POST['synopsis'];
  $leadtext = $_POST['leadtext'];
  $url = $_POST['url'];
  
  $name = htmlentities($name, ENT_COMPAT, 'UTF-8', false);
  $date = htmlentities($date, ENT_COMPAT, 'UTF-8', false);
  $time = htmlentities($time, ENT_COMPAT, 'UTF-8', false);
  $bline = htmlentities($bline, ENT_COMPAT, 'UTF-8', false);
  $synopsis = htmlentities($synopsis, ENT_COMPAT, 'UTF-8', false);
  $leadtext = htmlentities($leadtext, ENT_COMPAT, 'UTF-8', false);
  $url = htmlentities($url, ENT_COMPAT, 'UTF-8', false);
  
  $xml-&gt;program-&gt;addChild('name', $name);
  $xml-&gt;program-&gt;addChild('date', $date);
  $xml-&gt;program-&gt;addChild('start_time', $time);
  $xml-&gt;program-&gt;addChild('b-line', $bline);
  $xml-&gt;program-&gt;addChild('synopsis', $synopsis);
  $xml-&gt;program-&gt;addChild('leadtext', $leadtext);
  $xml-&gt;program-&gt;addChild('url', $url);
  
  $doc = new DOMDocument('1.0');
  $doc-&gt;formatOutput = true;
  $doc-&gt;preserveWhiteSpace = true;
  $doc-&gt;loadXML($xml-&gt;asXML(), LIBXML_NOBLANKS);
  $doc-&gt;save('data.xml');
  </code></pre>
  
  <p>I have searched for hours without finding exactly what I'm looking for. Also tried my best to try plenty of my own "not-working-solutions". Also, I picked out the parts of my code that's relevant for answers on this matter.</p>