Kevin Aartsen Kevin Aartsen - 1 month ago 5
HTML Question

Can't $_POST with ckeditor

I'm having trouble with CKEDITOR and i was hoping you could help me out.

I have this code inside a form tag:

<div class="column column-1">
<h3>Content</h3>
<textarea id="editor1" name="editor1" type="text"></textarea>
</div>


I applied the next javascript to make the ckeditor work:

CKEDITOR.replace("editor1");


So far so good the editor is showing in my browser i can type in it and all that good stuff.

Now i tried to work with it using PHP:

echo $_POST['editor1'];


If the user submits the form it should echo
editor1

The problem is its not happening. I tried to run multiple tests but its just not showing me anything. If i do the same with a normal textarea (not a ckeditor one) just the standard textarea it works. WHen i do it with ckeditor it wont work.

Answer

I've run into this issue myself, I'm not sure my solution is 'best practice' but it does seem to work without issues.

What I've done is added an option-setting when initiating the editor to caputre the input content 'on change' and add it to your original textarea (which ckeditor will 'hide' before initiating itself).

The code:

CKEDITOR.replace( 'editor1', {
        on : { 
            change: function ( evt )  {
              $('textarea[name="editor1"]').html(evt.editor.getData());
            }
        }
}); 

Notice the 'change' event-listener that updates the hidden text area.

Alternativly, you should be able to use the getData() function to retrieve the editor contents when the form is submitted (e.g. if you are using ajax etc.).

Hope this helps.