views:

32

answers:

1

I'm using CKEditor to created the portion of my CMS for the user to input content. My CMS is a bar/menu at the top with the sections of the site for the user to create, update, or delete an entry.

When the user selects an option I send the request for the form items to php using jquery AJAX $.post. The function returns the code and I use $('#loadCMS').html(data) to create the form without reloading the page. This work great and debugging always shows the correct code returned.

However, CKEditor only loads the first time an item is selected. It may load again but it's rare.

CKEditor is javascript that sits in the head and replaces specified textareas with the editor

<head>
...
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
...
</head

what is loaded dynamically to call the editor

<textarea name="editor1"></textarea>
    <script type="text/javascript">
        CKEDITOR.replace( "editor1",
        {
            toolbar :
            [
            ['Source'],
            ['Cut','Copy','Paste','PasteText','PasteFromWord','-', 'SpellChecker'],
        ['Undo','Redo','-','RemoveFormat'],
        ['Bold','Italic','Underline'],
        ['Subscript','Superscript'],
        ['NumberedList','BulletedList'],
        ['Link','Unlink'],
        ['Image','Flash','HorizontalRule','SpecialChar','Format'],
        ['Maximize', 'ShowBlocks','-','About']
            ],
            width : '1000',
            height : '300',
            filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
            filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?Type=Images',
            filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?Type=Flash'
        });
    </script>

jquery

$('#portfolioCreate').click(function()
    {
        var detailsList = new Array('title','medium','original');
        $.post('cms.php',{detailsList: detailsList,images:"imageOn",subimgNum:0,content1:"Comments"},
        function(data)
        {
            $('#loadCMS').html(data);
            $('#debug').val(data);
        });
    });

The forms are created dynamically every time. However, the textareas replaced by CKEditor does not always replace, it's just blank not even the textarea box shows. The first time a selection is made it works. If the user chooses to create a new blog entry the text area is replaced, if they then choose to update bio no text area is ever replaced even if they go back to create a new blog entry.

-----------------SOLUTION-------------------------

dynamic php

$key = md5(time().rand())
<textarea name="'.$key.'"></textarea>
<script type="text/javascript">
CKEDITOR.replace("'.$key'",
{
     .....
});
<input type="hidden" value="'.$key.'" name="content1Key" />
</script>

php to pull from form

$content1Key = $_POST['content1Key'];
$content1 = $_Post[$content1Key];
A: 

CKEditor is very picky about dynamically creating forms because of the way they store references (in one global array keyed by textarea name I believe). Try to give each textbox a unique name to avoid it trying to return an existing reference that's been destroyed/overwritten, and after loading each new form you need to call CKEDITOR.replace.

Ian Wetherbee
yeah, seems like that was the issue. however now each one works once.if I have have 3 editors I have 3 unique names, another option with a unique name works now but if I go back it wont work since it was "destroyed/overwritten" already. hummmm.
dcp3450
so, basically CKEditor is retaining the name of the textarea to prevent data from being overwriten. When the page reloads the data is lost allowing the textarea to be edited again. I just have to figure out how to force CKEditor not to do that or tell it to release them textarea names when a new item is selected.
dcp3450
Are the form names unique between form refreshes? Each time you fetch the form it needs to have a unique name-try appending the current timestamp.
Ian Wetherbee
Just tried it. The form name isn't the dependent. the textarea name is. However, I need to know the text area name when I submit the form so I can post it to the php.
dcp3450
so, it loads fairly quick so if I have 3 editors they end up with the same timestamp as their name so only the first one works.
dcp3450
Sorry, yes I mean the textarea name. Then md5 the key, it just needs to be random: `md5(time() . rand())`. Simply add an `<input type="hidden" value="1928374712" name="timestamp" />`, then get the textarea in PHP by: `$_POST["editor" . $_POST["timestamp"]];` and you can get the dom element using jQuery: `$("[name=editor" + $("[name=timestamp]").val() + "]")`
Ian Wetherbee
ok, so for each instance of the CKEditor I created $key = md5(time().rand()) and set its name to $key. I just had to create a new key each time an instance is called. Now, I could have anywhere form 0-3 instances that will be passed to php when the user submits. So, I all have to do is have a hidden textfield that stores that instances $key.
dcp3450
for the record using Ian Wetherbee's, I'll put my solution above. Thanks!
dcp3450