2016-01-27 15 views
0

我試圖創建一種方法來更新我的網站在前端。我已經添加了PHP代碼的以下內容可編輯區域加載.txt文件的內容:頁面未被加載.txt文件更新

<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."; 
     } 
?> 

後,我更新了以下AJAX和PHP代碼的文件,內容是沒有改變的現場。即使在重裝後也是如此。我仍然看到舊內容。不知道爲什麼 - 當我看到.txt文檔的內容時,我確實看到它們已被更改。

阿賈克斯:

<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編輯txt文件(其中還記得正常工作):

<?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; 
} 
?> 

我沒有線索什麼可以發生在這裏。提前致謝!

亞特蘭特A.

回答

0

我的壞,我沒有看到它的正確更早之前發佈但是這個代碼做事情很糟糕:

$content = stristr($content, null); 

刪除它使人們正常工作。如果有人能解釋爲什麼我會欣賞它!