Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the current content of codemirror

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 comments or suggestions are highly appreciate. Thanks.

like image 333
Zhiyong Zhang Avatar asked Oct 09 '22 02:10

Zhiyong Zhang


2 Answers

Note 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();});
like image 162
Mrnt Avatar answered Oct 13 '22 09:10

Mrnt


The last code with editor ... You have to put in a function and call it after changing the text area - maybe with OnBlur="".

like image 38
Dion Avatar answered Oct 13 '22 11:10

Dion