dqlb38410 2016-01-27 00:04
浏览 28
已采纳

.txt文件更新后页面未加载

I'm trying to create a way to update my site on the front end. I've added the following content editable area with php code to load the contents of the .txt file:

<div class="content" contenteditable="true">

    <?php
    //echo file_get_contents("test.txt");
        $file = dirname(__FILE__).'/siteContent/'. $_GET["page"]. '.txt';
        $content = file_get_contents( $file );
        $content = stristr( $content, null );
        if($content != ""){
        echo utf8_encode( $content );
        }else{
            echo "Empty page. Please erase this text and replace with your own.";
        }
?>

After I update the file with the following ajax and php code, the content isn't changed on the site. Even after a hard reload. I still see the old content. Not sure why--when I see the contents of the .txt documents I do see that they have been changed.

Ajax:

<script>

$('.content[contenteditable=true]').on('blur',function(){

    var data = $( "div.content" ).html();
    var pageEdit = <?php echo '"'. $_GET["page"] . '"'; ?>; 
    $.ajax({
    url: 'process/edit.php',
    type: 'POST',
    data: { data: data, pageToEdit: pageEdit },
    success: function(result) {
       alert('the data was successfully sent to the server');
       console.log(pageEdit);
    }
 });

});

</script>

PHP edit .txt file (which remember works fine):

<?php
if ($_SERVER['REQUEST_METHOD']!='POST') {
    echo 'Not a POST request';
    exit;
}
$data = $_POST['data'];
$pageToEdit = $_POST['pageToEdit'];
if (empty($data)) {
    echo 'Post \'data\' was empty!';
    exit;
}
$filename=dirname(__FILE__).'/../siteContent/' . $pageToEdit . '.txt';
if (!file_exists($filename)) {
    echo 'Nav file cannot be found';
    exit;
}
if (!is_writable($filename)) {
    echo 'Nav file is not writable';
    exit;
}
$write_status=file_put_contents($filename,$data);

if ($write_status === FALSE) { 
    echo 'We couldn\'t write the content to the file';
    exit;
}else{
    echo 'write was successfully written to: '. $filename;
    exit; 
}
?>

I don't have a clue as to what could be happening here. Thanks in advance!

Atlante A.

  • 写回答

1条回答 默认 最新

  • doudou5023 2016-01-27 00:13
    关注

    My bad, I didn't see it properly earlier prior to posting but this code made things go pretty bad:

    $content = stristr( $content, null );
    

    Removing it made it work properly. If someone could explain why I'd appreciate it!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流