我使用 ckEditor 的内联编辑功能和 jQuery 的 Ajax(从 oleq 学习)来说明我遇到的问题。如果我只有一个编辑器,它工作得很好。从文件中读取文本,然后通过周期性 ajax 函数将文本写回文件。以下代码显示了这一点。
<?php
if (!is_file('textFile.txt')) {
file_put_contents('textFile.txt', 'This is the contents of file textFile.txt');
}
if (count($_POST)) {
//retrieve data from POST write to the file
if (isset($_POST['textFile'])) {
file_put_contents('textFile.txt', $_POST['textFile']);
}
exit;
}
?>
<script type="text/javascript" src="./css/jQuery.js"></script>
<script src="./ckeditor/ckeditor.js"></script>
<script>
// The "instanceCreated" event is fired for every editor
CKEDITOR.on('instanceCreated', function(event) {
var editor = event.editor,
element = editor.element;
});
</script>
<div id="container">
<p id="text" contenteditable="true">
<?php echo file_get_contents('textFile.txt'); ?>
</p>
<script>
CKEDITOR.disableAutoInline = true;
var editor = CKEDITOR.inline('text', { on: {
instanceReady: function() {
periodicText();
}
}});
CKEDITOR.disableAutoInline = false;
var periodicText = (function() {
var data, oldData;
return function() {
if ((data = editor.getData()) !== oldData) {
oldData = data;
console.log(data);
$.post("help.php", {
textFile:data
}
);
}
setTimeout(periodicText, 1000);
};
})();
</script>
</div>
我为 2 个编辑器编写的第二个脚本不起作用。提前感谢您告诉我我做错了什么。:)
<?php
if (!is_file('textFile.txt')) {
file_put_contents('textFile.txt', 'This is the contents of file textFile.txt');
}
if (!is_file('foxFile.txt')) {
file_put_contents('foxFile.txt', 'The quick brown fox jumps over the lazy dog.');
}
if (count($_POST)) {
//retrieve data from POST write to the file
if (isset($_POST['textFile'])) {
file_put_contents('textFile.txt', $_POST['textFile']);
}
if (isset($_POST['foxFile'])) {
file_put_contents('foxFile.txt', $_POST['foxFile']);
}
exit;
}
?>
<script type="text/javascript" src="./jQuery/jQuery.js"></script>
<script src="./ckeditor/ckeditor.js"></script>
<script>
// The "instanceCreated" event is fired for every editor
CKEDITOR.on('instanceCreated', function(event) {
var editor = event.editor,
element = editor.element;
});
</script>
<div id="container">
<p id="text" contenteditable="true">
<?php echo file_get_contents('textFile.txt'); ?>
</p>
<script>
CKEDITOR.disableAutoInline = true;
var editor = CKEDITOR.inline('text', { on: {
instanceReady: function() {
periodicText();
}
}});
CKEDITOR.disableAutoInline = false;
var periodicText = (function() {
var data, oldData;
return function() {
if ((data = editor.getData()) !== oldData) {
oldData = data;
console.log(data);
$.post("bar.php", {
textFile:data
}
);
}
setTimeout(periodicText, 1000);
};
})();
</script>
<br><br><br><br>
<p id="text" contenteditable="true">
<?php echo file_get_contents('foxFile.txt'); ?>
</p>
<script>
CKEDITOR.disableAutoInline = true;
var editor = CKEDITOR.inline('text', { on: {
instanceReady: function() {
periodicFox();
}
}});
CKEDITOR.disableAutoInline = false;
var periodicFox = (function() {
var data, oldData;
return function() {
if ((data = editor.getData()) !== oldData) {
oldData = data;
console.log(data);
$.post("bar.php", {
foxFile:data
}
);
}
setTimeout(periodicFox, 1000);
};
})();
</script>
</div>