I'd like to save the current content of a codemirror editor using a php script. I used the following codes in the hope when clicking on a button the current content will be passed to the php script. The scripts below has been edited and worked.
var test = code.getValue();
but this does not reflect the change in the editor.
<script language = "Javascript">
function saveData() {
var test = editor.getValue();
new Ajax.Request('savedata.php', {
method: 'post',
parameters: {
test: test,
}
});
}
</script>
The editor is constructed using: ;
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
lineWrapping: true,
extraKeys: {"F11": toggleFullscreenEditing, "Esc": toggleFullscreenEditing},
onBlur: function(){editor.save()}
});
Any ments or suggestions are highly appreciate. Thanks.
I'd like to save the current content of a codemirror editor using a php script. I used the following codes in the hope when clicking on a button the current content will be passed to the php script. The scripts below has been edited and worked.
var test = code.getValue();
but this does not reflect the change in the editor.
<script language = "Javascript">
function saveData() {
var test = editor.getValue();
new Ajax.Request('savedata.php', {
method: 'post',
parameters: {
test: test,
}
});
}
</script>
The editor is constructed using: ;
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
lineWrapping: true,
extraKeys: {"F11": toggleFullscreenEditing, "Esc": toggleFullscreenEditing},
onBlur: function(){editor.save()}
});
Any ments or suggestions are highly appreciate. Thanks.
Share Improve this question edited Jan 1, 2012 at 19:20 Zhiyong Zhang asked Jan 1, 2012 at 2:48 Zhiyong ZhangZhiyong Zhang 531 silver badge4 bronze badges3 Answers
Reset to default 3Note that the CodeMirror's event api has changed (see CodeMirror doc)
So the above should be something like:
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
lineWrapping: true,
extraKeys: {"F11": toggleFullscreenEditing, "Esc": toggleFullscreenEditing},
});
editor.on("blur": function(){editor.save();});
The last code with editor ... You have to put in a function and call it after changing the text area - maybe with OnBlur=""
.
this will also work:
document.querySelector('.CodeMirror').CodeMirror.setValue('some text');