doremi
doremi

Reputation: 15329

POSTing CKEditor Contents

I have a web form that I'm $.posting that contains a CKEditor textarea.

    $(document).ready(function() {

        CKEDITOR.replace('html');
        CKEDITOR.config.htmlEncodeOutput = true; //seems to have no effect

        $('#save').click(function() {            

            $.post('/async.php?a=save-slide', $('#slideForm').serialize(),
            function(json) {
                console.log(json);
            }, 'json');
        });            
    });

I have two problems:

  1. .serialize() isn't getting the CKEditor contents. If I console.log the serialized string, html= is empty.
  2. If I use the CKEditor getData() method and there is an ampersand ( ) in the POSTed content, my script breaks because it's making an XML-based API call.

Any ideas on how I can get the contents and safely POST xml-friendly data?

Upvotes: 1

Views: 2301

Answers (1)

ScottE
ScottE

Reputation: 21630

I use the following generic method to move ckeditor contents back into the text area they were attached to:

   var $editors = $("textarea.editor");
   if ($editors.length) {
       $editors.each(function () {
           var instance = CKEDITOR.instances[this.id];
           if (instance) { $(this).val(instance.getData()); }
       });
   }

If your situation is simpler there is no need for the loop.

There is also the jquery helper that is handy for this sort of thing.

Upvotes: 1

Related Questions