简体   繁体   中英

Html form to PHP XML DOMDocument - Not Updating XML

I am new to DOMDocument and intensive PHP. Please forgive my ignorance. I just cant seem to get the Process.php to write to the XML. Ive seen other posts about the same topic, but they didnt help me resolve my issue. Just cant find what I did wrong. Any help would be much appreciated!

Here is the HTML form, form.html.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<form name="digitalsignage" action="process.php" onsubmit="return defaultagree(this)" method="post">
<table align="center" cellpadding="2" cellspacing="0" width="50%">
  <tbody><tr>
    <td> Event1:</td><td>
<input name="event1" value="Unknown" type="text">    </td>
    <td> Event2:</td><td>
<input name="event2" value="Unknown" type="text">    </td>
    <td> Event3:</td><td>
<input name="event3" value="Unknown" type="text">    </td>
    <td> Event4:</td><td>
<input name="event4" value="Unknown" type="text">    </td>
    <td> Event5:</td><td>
<input name="event5" value="Unknown" type="text">    </td>
  </tr>
  <tr>
     <td colspan="4" style="text-align:center;">
<input name="lsr-submit" value="Submit" type="submit"></td>
  </tr>
</tbody></table>
</form>
</body>
</html>

The XML File, file.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://www.SERVER-URL-OF-FEED" rel="self" type="application/rss+xml" />
<title>Todays Events</title>
<item>
  <description>
        <event1>Event 1</event1>
        <event2>Event 2</event2>
        <event3>Event 3</event3>        
        <event4>Event 4</event4>
        <event5>Event 5</event5>
  </reports>
</item>
</channel>
</rss>

The PHP, process.php

<?php

$event1 = $_POST['event1'];
$event2 = $_POST['event2'];
$event3 = $_POST['event3'];
$event4 = $_POST['event4'];
$event5 = $_POST['event5'];

$xml = new DOMDocument();
$xml->load('file.xml');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;

$element = $xml->getElementsByTagName('description')->item(0);

if ($element->length > 0) {

    $event1 = $element->getElementsByTagName('event1')->item(0);
    $event2 = $element->getElementsByTagName('event2')->item(0);
    $event3 = $element->getElementsByTagName('event3')->item(0);
    $event4 = $element->getElementsByTagName('event4')->item(0);
    $event5 = $element->getElementsByTagName('event5')->item(0);

    $newItem = $xml->createElement('description');

    $newItem->appendChild($xml->createElement('event1', $_POST['event1']));
    $newItem->appendChild($xml->createElement('event2', $_POST['event2']));
    $newItem->appendChild($xml->createElement('event3', $_POST['event3']));
    $newItem->appendChild($xml->createElement('event4', $_POST['event4']));
    $newItem->appendChild($xml->createElement('event5', $_POST['event5']));

    $xml->getElementsByTagName('item')->item(0)->appendChild($newItem);
}

$xml->save('file.xml');

echo "Data has been written.";

?>

Some more basic issues to spot with your code:

  1. No error handling. That's something to really look after. Your code is written with "best wishes" and in the hope that everything just works. In reality, when is that? So yeah, production code needs error handling. Take notice when you find code examples that these are often examples and do not focus on error handling so you need to add it yourself. Add error handling as early as possible ( fail early, fail often, fail fast )
  2. Code duplication. If you look at you example, it becomes clear that there is a lot of redundancy. For the sheer example there is no difference if there one field or the five fields you've got. Posting on Stackoverflow requires you to minimize the example to as little as necessary to ask the quesiton. You've broken that contract. But that's not to make it harder for you to ask a question on Stackoverflow it's a nice point to remove redundancy from your own code. Take the time to ask a question here as an opportunity to review your code from a different perspective because you need to formulate a more independent question. You might even solve the problem yourself by doing so. And that's a good sign.

$result = $xml->save('file.xml');
if (!$result) {
    throw new UnexepectedValueException(
       'Failed to write file "file.xml"'
    );
}

echo "\n<hr>\nthe following file has been written:\n<hr>\n";
echo nl2br(htmlspecialchars($xml->saveXML())), "\n<hr>\n";

Imagine as many possibilities as possible to make visible and "proof" your code working, showing errors as early as possible.

You could...

  • ... test if a file in place is accessible as writeable before writing to it
  • ... verify the XML written to disk is well-formed
  • ... verify the HTTP POST request contains all expected variables
  • ... verify the variables contain values you're fine to deal with
  • ... check if there was actually a request at all
  • ... .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM