Can you provide an example on how to setup CKEditor to save via AJAX using the Save button in the CKEditor toolbar?
I\'m interested in creating a CKEditor AJAX save
I have posted the simplest ajax save plugin here Ajax save plugin for CKEDITOR 3.x with jquery 1.4.x
<textarea cols="80" id="editor1" name="editor1" rows="10"></textarea>
<button id="save" onclick="save()"></button>
<script type="text/javascript">
function save() {
var question = CKEDITOR.instances.editor1.getData();
alert(question);
$.ajax({
type: 'POST',
data: {file: question},
url: "aiq_save.php?xyz="+question+"",
success: function (html) {
alert('may be saved');
$("#show").html(html);
}
});
return false;
</script>
<div id="show"></div>
Create a new page aiq_save.php
, then:
<?php
ECHO $_GET['xyz'];
ECHO $_POST['file'];//post method
?>
And you've done it.
This is the method I use, no plugins required:
It's simple and reliable and uses the CKEditors built in save button.
Add a non-visible submit button (display:none) to the same form where the CKEditor is and set it's ID and Name to "submit" then both the input's onclick and the forms onsubmit will both be executed when the CKEditor's standard save button is clicked. you can hook up your event handlers inline or with jquery.bind() or any other way. Then add a function attached to the forms onsubmit event to serialize the form and ajax post it to the url set in the form 'action' attribute. Just return 'False' from the event handler to ensure the form does not post. Now any code or button (including the ckeditor save button) that submits the form will run your handler instead. No CKeditor plugins or CKeditor configuration required. Here's a simplified example (assumes JQuery ).
<form id="myform" name="myform" action="" method="post" onsubmit="alert('form.onsubmit()');return false;">
<input type="submit" id="submit" name="submit" style="display:none" onclick="SaveText(this);"/>
<textarea id="ckeditor1"></textarea>
</form>
<script>
function SaveText (eventargs){
var form = $(eventargs).closest("form");
var data = form.serialize();
var url = form.attr('action');
$.post(url, data);
return false;
}
</script>
A more realistic approach might use JQuery.Bind() and the script would be in an external JS file etc. but the end result is the same. It works because the input hides the form's submit function so any call to form.submit() calls the submit button's onclick() function instead (std behavior for all browsers). Because it's a 'submit' button it causes the form's 'onsubmit' event to fire which would not normally happen if you called form.submit() directly. so you get reliable loosely coupled interception of the save event without plugins or using the CKEditor API. you can use it for more than ajax save too, its nice for any pre-save processing you need to do.
I've tried Korikulum's method on CKEditor 4 but it posts the form twice so I've come up with this:
$(function () {
setTimeout(function () {
CKEDITOR.instances.MyEditor.on('beforeCommandExec', function (event) {
if (event.data.name === 'save') {
event.cancel();//this does not seem to prevent the form post
$(MyEditor).val(CKEDITOR.instances.MyEditor.getData());//copy data from the editor to the textarea
$.ajax({
type: $(editorForm).attr('method'),
url: $(editorForm).attr('action'),
data: $(editorForm).serialize(),
success: function (data) {
alert(data.message);
}
});
}
});
}, 100);//CKEditor is heavy and needs time to initialize
editorForm.submit = function (e) {//this is needed to prevent the 2nd post
return false;
};
});
MyEditor is the id of the text area with ckeditor class
editorForm is the id of the form that wraps the text area
CKEditor overrides form.submit() function when it's initialized within a form and event.cancel() does not seems to prevent the form being posted. So I had to override the function with one that just returns false.
EDIT: I forgot to copy the newly edited data to the textarea so it could be sent along with the rest of the form.