开发者

PHP not saving with DOMDocument

We'll start off straight forward with the code:

The PHP:

<?php
    $newJaCourse = $_POST["ja-new-course"];
    $newJaSide = $_POST["ja-new-side"];
    $newEnCourse = $_POST["en-new-course"];
    $newEnSide = $_POST["en-new-side"];

    $doc = new DOMDocument('1.0','utf-8');
    $doc->formatOutput=true;
    $doc->preserveWhiteSpace=false;
    echo $doc->load("../../xml/daily_lunch.xml") . "Loaded <br />";

    $japanese = $doc->getElementsByTagName("japanese")->item(0);
    $jacourse = $japanese->getElementsByTagName("course")->item(0);
    $jaside = $japanese->getElementsByTagName("side")->item(0);
    $english = $doc->getElementsByTagName("english")->item(0);
    $encourse = $english->getElementsByTagName("course")->item(0);
    $enside = $english->getElementsByTagName("side")->item(0);

    $jacourse->nodeValue = $newJaCourse;
    $jaside->nodeValue = $newJaSide;
    $encourse->nodeValue = $newEnCourse;
    $enside->nodeValue = $newEnSide;

    $japanese->replaceChild($jacourse,$jacourse);
    $japanese->replaceChild($jaside,$jaside);
    $english->replaceChild($encourse,$encourse);
    $english->replaceChild($enside,$enside);

    echo $doc->save("../../xml/daily_lunch.xml") . "Done!";
?>

My exquisite HTML Form:

        <h4>Change Today's Lunch Menu:</h4>
        <form method="post" action="scripts/lunchupdate.php" name="changelunch">
            <table>
                <tr>
                    <th>Japanese: Course</th>
                    <td><input type="textbox" name="ja-new-开发者_C百科course" /></td>      
                </tr>
                <tr>
                    <th>Japanese: Side</th>
                    <td><input type="textbox" name="ja-new-side" /></td>        
                </tr>
                <tr>
                    <th>English: Course</th>
                    <td><input type="textbox" name="en-new-course" /></td>      
                </tr>
                <tr>
                    <th>English: Side</th>
                    <td><input type="textbox" name="en-new-side" /></td>        
                </tr>
            </table>
            <a href="javascript:" onclick="writeNewLunch()">Set Menu</a> |
            <a href="javascript:" onclick="document.changelunch.reset()">Reset Input</a>
        </form>

My Current XML:

<?xml version="1.0" encoding="UTF-8"?>
<lunch featuredisabled="false">
    <japanese>
        <course>
            えびトマトクリームパスタ
        </course>
        <side>
            シーザーサラダ
        </side>
    </japanese>
    <english>
        <course>
            Shrimp and Tomato Cream Pasta
        </course>
        <side>
            Ceasar Side Salad
        </side>
    </english>
</lunch>

Although you can probably tell the purpose, it is to change the items in an XML file. A daily lunch updater for a bilingual restaurant is the goal. My wonderful DOMDocument->save(filename) doesn't do jack though.

Nothing is returned, all my variables and changes echo. I am running PHP5. I looked in phpinfo() and found where all the XML requirements are configured, turned on and not doing their job.

The only thing that comes output form this script is:

Loaded

Done!


Filename is missing. According to the PHP documentation you should have

$doc->save('myfile.xml');

Do you have permissions to write into folder?


I think $doc->save() needs to have the file name passed. So replace $doc-save() with $doc->save("../../xml/daily_lunch.xml")

http://php.net/manual/en/domdocument.save.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜