Export XML from form data [closed]

南楼画角 提交于 2020-01-17 13:53:30

问题


I'm trying to come up with a general routine for exporting form data to an XML format. The goal would be to make it general enough that the form element IDs would serve as the XML nodes, and the form values would obviously be the node content. Here's what I have so far:

if(isset($_POST['submit'])) {
 $doc = new DOMDocument('1.0');
 $doc->formatOutput = true;
 $post = $_POST;
 unset($post['submit']);
 $data = array_values($post);
 $headers = array_keys($post);
 /* TODO - wrap next two lines with if clause */
 /* to check for existing root node */
 $root = $doc->createElement('student');
 $root = $doc->appendChild($root);

 for ($i = 0; count($headers) - 1; $i++)    {
    /* Create and append node for each form element to root */
    $theNode = $doc->createElement($headers[$i]);
    $theNode = $root->appendChild($theNode);

    /* Add content to node */
    $theValue = $doc->createTextNode($data[$i]);
    $theValue = $theNode->appendChild($theValue);
 }
 $fh = fopen($file, 'w') or die("Can't open the XML file.");
 fwrite($fh, $doc->saveXML());
 fclose($fh);
 header('Location: thanks.php'); 
}

And, as requested, here's the form code, although I think that's irrelevant:

          <form name="form1" method="post" action="">
        <p>
          <label for="name">Name: </label>
          <input type="text" name="name" id="name" placeholder="Your full name" autofocus required>
        </p>
        <p>
          <label for="email">Email: </label>
          <input type="email" name="email" id="email">
        </p>
        <p>
          <label for="cell">Cell: </label>
          <input type="tel" name="cell" id="cell">
        </p>
        <p>
          <label for="dob">Date of birth: </label>
          <input type="date" name="dob" id="dob">
        </p>
        <p>
          <label for="study">Years of art study: </label>
          0 <input type="range" name="study" id="study" min="0" max="16"> 16
        </p>
        <p style="text-align: center;">
          <input type="submit" name="submit" id="submit" value="Submit">
        </p>
      </form>

Unfortunately, this code results with an HTTP Error 500. Any ideas on what I'm doing wrong?

Oh, and as the TODO notes, I need to also figure out if there already is data (a root node exists) in the file, just append and don't reinsert the root node. How do I test for a root node in an XML file?

Thanks so much for your help - Joe


回答1:


Here's a code snippet that'll solve the 1st part of your problem. I added a hard-coded $_POST with dummy values to simulate your form submission:

<?php
$_POST = array(
    'submit' => true,
    'name'   => 'Name',
    'email'  => 'Email',
    'cell'   => 'Cell',
    'dob'    => 'Date of birth',
    'study'  => 'Years of art study'
);

if(isset($_POST['submit'])) {
    $doc               = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $root              = $doc->createElement('student');
    $root              = $doc->appendChild($root);

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

    foreach ($post as $key => $value) {
        $node = $doc->createElement($key, $value);
        $root->appendChild($node);
    }

    echo $doc->saveXML();
}

Output:

<?xml version="1.0"?>
<student>
  <name>Name</name>
  <email>Email</email>
  <cell>Cell</cell>
  <dob>Date of birth</dob>
  <study>Years of art study</study>
</student>

For the 2nd part of your problem, the DOMDocument::saveXML function doesn't prompt you to download a file or saves a file to disk, instead as its description states:

DOMDocument::saveXML — Dumps the internal XML tree back into a string

Assuming you want to be able to download the resulting XML take a look at the answer to this question here in SO: How do I create an XML file with php and have it prompt to download?.



来源:https://stackoverflow.com/questions/17604658/export-xml-from-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!