0

I am doing some php programming with jquery adapter and ckeditor on my page. Let me show what I have. The page I am working on is index.php

A jquery adapter:

    function sendid(id)
     {
    jQuery('#mydiv').showLoading();
    $.ajax({
        type: "POST",
        url: "",
        data: id,
        error: function(){

          alert('error');
      },
      success: function(data){
        jQuery('#mydiv').hideLoading();
            $('#mydiv').html(data);
  }
  });
}
}

This function sends an id to index.php . Whith this Id I am fetching my database and get some records. With the record I got, I am displaying it via ckeditor like this:

          <?php
            $ckeditor = new CKEditor();
            $ckeditor->basePath  = 'ckeditor/' ;
            CKFinder::SetupCKEditor( $ckeditor, 'ckfinder/' ) ;
            $config['height'] = '300';
            $initialValue = $queryresult['content'];
            $ckeditor->editor('FCKeditor1', $initialValue, $config);
        ?>

Whenever I click the button which calls sendid() function ckeditor appears above the list of ids. The first time when I call sendid() function it works ok and puts the record into ckeditor. However second time I call the sendid() function ckeditor disappears.

I found a topic in this link:

CKEditor instance already exists

but it becomes very hard for me, where to place the codes mentioned in the link. As far as I understood I have to kill or destroy the editor everytime I click the button for sending the id to sendid() function. But whenever I put destroy or kill ckeditor into sendid() function it did not work.

Could you please help me on this.

Community
  • 1
  • 1
ilhank
  • 148
  • 6
  • 25

2 Answers2

2

Please, download the latest CKEditor from http://ckeditor.com/download. Unpack it on your server so you can run it, for example http://localhost/ckeditor_test/ (anywhere you like).

Now, save the following code in the same directory where all the editor files have been unpacked (ckeditor.js, ckeditor_php5.php and so on) as index.php.

Open the webiste and enjoy clicking the button. This sample (sandbox, actually) will let you understand how to replace an editor dynamically and destroy existing instances to avoid conflicts.

<?php if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ): ?>
    <?php
        include( 'ckeditor_php5.php' );

        $ckeditor = new CKEditor();
        $ckeditor->editor( $_POST[ 'id' ], 'This is my initial value!', array(
            'height' => '300'   // Config to be used
        ));
    ?>
<?php else: ?>

    <!DOCTYPE html>
    <html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    </head>
    <body>
        <div id="mydiv" style="outline: 2px dashed orange; padding: 20px; margin: 0 0 20px;">
            This is where the editor will be loaded.
        </div>
        <button id="load" type="submit">Load the editor via AJAX!</button>
        <script>

            (function( $ ) {
                var id = 'idOfTheEditor';

                $( '#load' ).on( 'click', function() {
                    $.ajax({
                        type: 'POST',
                        url: 'index.php',
                        data: {
                            id: id
                        },
                        error: function(){
                            console.log( 'Error?' );
                        },
                        success: function( data ) {
                            if ( window.CKEDITOR && CKEDITOR.instances[ id ] ) {
                                console.log( 'Desytroying an existing instance!' );
                                CKEDITOR.instances[ id ].destroy();
                            }
                            $( '#mydiv' ).html( data );
                        }
                    });
                });

            })( jQuery );

        </script>
    </body>
    </html>
<?php endif ?>

You can eventually wrap $( '#mydiv' ).html( data ) in setTimeout closure like this to see your code working in "slow motion":

setTimeout( function() {
        $( '#mydiv' ).html( data );
}, 1000 );

Have fun!

oleq
  • 15,697
  • 1
  • 38
  • 65
  • oleq thank you so much,it worked. what I did is based on what you sent me, I modified the jquery adapter like: function sendid(id) { jQuery('#mydiv').showLoading(); $.ajax({ type: "POST", url: "", data: "id="+id, error: function(){ alert('error'); }, success: function(data){ jQuery('#mydiv').hideLoading(); if ( window.CKEDITOR && CKEDITOR.instances[ id ] ) { CKEDITOR.instances[ id ].destroy(); $('#mydiv').html(data); } }); } } – ilhank Sep 18 '12 at 12:25
  • You're welcome! Make sure your id is passed properly. Also `jQuery === $`, so `('#mydiv')` can be `$('#mydiv')` if you don't use any other libraries which could overwrite `$` namespace. – oleq Sep 18 '12 at 12:39
1

I believe your jQuery code should look like this for the patch to work:

function sendid(id)
     {
    jQuery('#mydiv').showLoading();
    $.ajax({
        type: "POST",
        url: "",
        data: id,
        error: function(){

          alert('error');
      },
      success: function(data){
        jQuery('#mydiv').hideLoading();

        var instance = CKEDITOR.instances['FCKeditor1'];
        if(instance) {
            instance.destroy(true);
        }
        CKEDITOR.replace('FCKeditor1');

        $('#mydiv').html(data);
  }
  });
}
}
Zathrus Writer
  • 4,311
  • 5
  • 27
  • 50