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.