无法删除 XML 文件

发布于 2024-10-01 22:12:17 字数 4215 浏览 4 评论 0原文

背景:

我的网页通过 AJAX/jQuery 从 XML 文件加载条目并显示它们(工作正常)

该页面还有一个表单,通过 AJAX/jQuery 提交到 PHP 文件,该文件写入 XML 文件,然后通过以下方式重新加载 XML 条目: AJAX/jQuery 并显示它们(工作正常)

我有以下奇怪的问题:

如果我通过 FileZilla 删除 XML 文件,或者尝试通过从本地计算机上传副本来覆盖 XML 文件,则条目在以下情况下根本不会更改我正在看我的页面。事实上,如果用新副本覆盖 XML 文件,当我在地址栏中导航到该文件时,我会看到新副本,例如: http://mysite.com/myxmlfile.xml,但是,在显示 XML 条目的页面中,旧条目(例如文件从未被覆盖/删除)仍然显示!

使困惑!


这是我的 PHP 文件:

<?php

    header('Pragma: no-cache');
    header('Cache: no-cache; must-revalidate;');

    function signGuestbook($entry){

        $date = date("n.d.Y");
        $name = $entry['name'];
        $email = $entry['email'];
        $comment = $entry['comment'];

        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $doc->load('guestbook.xml');

        $root = $doc->firstChild;
        $e = $doc->createElement('entry');
        $dateNode = $doc->createElement('date', $date);
        $nameNode = $doc->createElement('name', $name);
        $emailNode = $doc->createElement('email', $email);
        $commentNode = $doc->createElement('comments', $comment);

        $e->appendChild($dateNode);
        $e->appendChild($nameNode);
        $e->appendChild($emailNode);
        $e->appendChild($commentNode);

        $root->appendChild($e);
        $doc->formatOutput = true;
        $doc->save('guestbook.xml');
    }

    function loadGuestbook(){

        $gBook = new DOMDocument();
        $gBook->load('guestbook.xml');

        $entries = $gBook->getElementsByTagName('entry');
        $entries_arr = array();

        foreach($entries as $entry)
        {
            $date    = $entry->getElementsByTagName('date')->item(0)->nodeValue;
            $name    = $entry->getElementsByTagName('name')->item(0)->nodeValue;
            $email   = $entry->getElementsByTagName('email')->item(0)->nodeValue;
            $comment = $entry->getElementsByTagName('comments')->item(0)->nodeValue;

            $entries_arr[] = array(
                'date'    => $date,
                'name'    => $name,
                'email'   => $email,
                'comment' => $comment
            );
        }

        return json_encode($entries_arr);
    }

    $entry = json_decode(stripcslashes($_POST['entry']), true);

    if($entry != null){

        signGuestbook($entry);
    }


    header("Content-type: text/plain");
    echo loadGuestbook();

    ?>

这是我的 JS 文件:

$(document).ready(function(){
    $.post('guestbook.php?' + new Date().getTime(), loadGuestbook, "text");
});

function loadGuestbook(gBook){

    var gBookDiv = $('div#guestbook');
    gBookDiv.empty();

    var entries = JSON.parse(gBook);

    $.each(entries, function(i, entry){

        gBookDiv.prepend(
            '<div class="entry">' +
            '<span class="date">' + entries[i].date + '</span>' +
            '<strong class="blue">Name:</strong><span class="name">' + entries[i].name+ '</span><br />' +
            '<strong class="blue">Email:</strong><span class="email">' + entries[i].email + '</span><br />' +
            '<strong class="blue">Comments:</strong><p>' + entries[i].comment + '</p>' +
            '</div>'
        );
    });
}

function signGuestbook(){

    var name    = $('input#name').val();
    var email   = $('input#email').val();
    var comment = $('textarea#comments').val();

    if(name == null || name == '' || email == null || email == ''){
        alert('You must provide a name and email.');
    }

    else{

        var entry = {
            "name"    : name,
            "email"   : email,
            "comment" : comment
        };

        var entryString = JSON.stringify(entry);
        $.post('guestbook.php?' + new Date().getTime(), { entry : entryString }, loadGuestbook, "text");

        $('input#name').val('');
        $('input#email').val('');
        $('textarea#comments').val('');
    }
}

Background:

My webpage loads entries from an XML file via AJAX/jQuery and displays them (works fine)

The page also has a form that submits via AJAX/jQuery to a PHP file which writes to the XML file, then reloads the XML entries via AJAX/jQuery and displays them (works fine)

I have the following odd problem:

If I delete the XML file through FileZilla, or try to overwrite the XML file by uploading a copy from my local machine, the entries do not change at all when I'm looking at my page. In fact, if overwrite the XML file with a new copy, I see the new copy if I navigate to it in my address bar like: http://mysite.com/myxmlfile.xml, however, in the page that displays the XML entries, the old entries (like the file was never overwritten/deleted) are still displayed!

Confused!


Here's my PHP file:

<?php

    header('Pragma: no-cache');
    header('Cache: no-cache; must-revalidate;');

    function signGuestbook($entry){

        $date = date("n.d.Y");
        $name = $entry['name'];
        $email = $entry['email'];
        $comment = $entry['comment'];

        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $doc->load('guestbook.xml');

        $root = $doc->firstChild;
        $e = $doc->createElement('entry');
        $dateNode = $doc->createElement('date', $date);
        $nameNode = $doc->createElement('name', $name);
        $emailNode = $doc->createElement('email', $email);
        $commentNode = $doc->createElement('comments', $comment);

        $e->appendChild($dateNode);
        $e->appendChild($nameNode);
        $e->appendChild($emailNode);
        $e->appendChild($commentNode);

        $root->appendChild($e);
        $doc->formatOutput = true;
        $doc->save('guestbook.xml');
    }

    function loadGuestbook(){

        $gBook = new DOMDocument();
        $gBook->load('guestbook.xml');

        $entries = $gBook->getElementsByTagName('entry');
        $entries_arr = array();

        foreach($entries as $entry)
        {
            $date    = $entry->getElementsByTagName('date')->item(0)->nodeValue;
            $name    = $entry->getElementsByTagName('name')->item(0)->nodeValue;
            $email   = $entry->getElementsByTagName('email')->item(0)->nodeValue;
            $comment = $entry->getElementsByTagName('comments')->item(0)->nodeValue;

            $entries_arr[] = array(
                'date'    => $date,
                'name'    => $name,
                'email'   => $email,
                'comment' => $comment
            );
        }

        return json_encode($entries_arr);
    }

    $entry = json_decode(stripcslashes($_POST['entry']), true);

    if($entry != null){

        signGuestbook($entry);
    }


    header("Content-type: text/plain");
    echo loadGuestbook();

    ?>

Here's my JS file:

$(document).ready(function(){
    $.post('guestbook.php?' + new Date().getTime(), loadGuestbook, "text");
});

function loadGuestbook(gBook){

    var gBookDiv = $('div#guestbook');
    gBookDiv.empty();

    var entries = JSON.parse(gBook);

    $.each(entries, function(i, entry){

        gBookDiv.prepend(
            '<div class="entry">' +
            '<span class="date">' + entries[i].date + '</span>' +
            '<strong class="blue">Name:</strong><span class="name">' + entries[i].name+ '</span><br />' +
            '<strong class="blue">Email:</strong><span class="email">' + entries[i].email + '</span><br />' +
            '<strong class="blue">Comments:</strong><p>' + entries[i].comment + '</p>' +
            '</div>'
        );
    });
}

function signGuestbook(){

    var name    = $('input#name').val();
    var email   = $('input#email').val();
    var comment = $('textarea#comments').val();

    if(name == null || name == '' || email == null || email == ''){
        alert('You must provide a name and email.');
    }

    else{

        var entry = {
            "name"    : name,
            "email"   : email,
            "comment" : comment
        };

        var entryString = JSON.stringify(entry);
        $.post('guestbook.php?' + new Date().getTime(), { entry : entryString }, loadGuestbook, "text");

        $('input#name').val('');
        $('input#email').val('');
        $('textarea#comments').val('');
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-10-08 22:12:17

这是一个缓存问题。为了防止 Ajax 调用被缓存,请在 Ajax 调用中添加时间:

$.post('myphp.php?' + new Date().getTime(), { my : dataString }, myFunction, "text");

This is a caching issue. To prevent your Ajax calls from being cached add the time to your Ajax call:

$.post('myphp.php?' + new Date().getTime(), { my : dataString }, myFunction, "text");
清君侧 2024-10-08 22:12:17

您可能还想在发送 XML 文件之前将其添加到 PHP 脚本中:

header('Pragma: no-cache');
header('Cache: no-cache; must-revalidate;');

You might also want to add this in your PHP script before sending the XML file:

header('Pragma: no-cache');
header('Cache: no-cache; must-revalidate;');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文