2

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-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!

2 Answers 2

6

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

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

Do you have permissions to write into folder?

Sign up to request clarification or add additional context in comments.

7 Comments

OH NO! I apologize to those answered so far! I have the filename in there too! That was a test run there. I updated the post. You can thank me for not checking my post.
And bingo with the permissions! However now that I changed it, I access this through...a webpage...ok we know that. I changed the permissions to that folder for everyone and there mom to have access to it. Currently on a VM, not a big deal, but when I take it online, how secure will that be?
I'm assuming that path is always going to remain hard coded and isn't changeable via the web page? If so I can't see a security issue, but I am often blind.
Yea it ain't going nowhere. We'll just save that for another day for yanking my hair out. I gotta let it grow out.
My opinion: If you have access to the database, save your lunch menu into the database. It will be more secure and you can have history of all changes. If you do not have any database, then saving into the file will probably be the only solution. Saving into file is secure, if users does not have direct access to it (it is on different location then your web page).
|
3

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

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.