Search code examples
ajaxckeditorhtmlspecialchars

ckeditor insert or update doesnt work in first try


I'm having some trouble in using CKEditor and send it's information to database. It works really good, I have all functionalities but when I sent it over ajax request to PHP, it doesn't assume the text into CKEditor first time. It only works after I make the ajax request the second time. It seems that CKEditor is not realtime and only after X time it saves the information that I typed, but I can't find anything related to it.

My init:

        if (jQuery('#js-ckeditor:not(.js-ckeditor-enabled)').length) {
        CKEDITOR.replace('js-ckeditor');

        // Add .js-ckeditor-enabled class to tag it as activated
        jQuery('#js-ckeditor').addClass('js-ckeditor-enabled');
    }

My Function to make the request:

        $.ajax({ url: '../inc/call.php',
        enctype: 'multipart/form-data',
        processData: false,
        contentType: false,
        cache: false,
        data: formData,
        type: 'POST',
        beforeSend: function(){
            $("#article_btn").html('<i class="fa fa-cog fa-spin"></i>');
            $("#article_btn").attr("disabled", true);
        },
        success: function(output) {...}});

One more question, what's the best way to save this information to database? Currently I'm using htmlspecialchars and htmlspecialchars_decode, but when using some functions (tables, colors), it really doesnt work well and don't save everything the correct way in database.


Solution

  • I will answer your first question:

    If you are using AJAX please update textarea manually before sending data to server by calling https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-updateElement. This topic is actually known. Please see e.g. https://dev.ckeditor.com/ticket/9913#comment:2.