1

I have install CKEDITOR to output text.Now i want to retrieve this text from SQL DB and insert into CKEDITOR for changes and updates.The value that I need to pass is $post_article.`The code below:

<div class="form-group" id="article">
    <label>ΚΕΙΜΕΝΟ</label>
  <textarea class="form-control" name="post_article"  cols="30" rows="20"></textarea>
        <script>CKEDITOR.replace( 'post_article' );
 CKEDITOR.instances.post_article.setData( '<p><?php echo $post_article;?</p>');
       </script>
 </div>

Unfortunately it doesn't work.Any suggestions or solutions? Thanks.

liontass
  • 730
  • 9
  • 24
  • So set it to a textarea and set ckeditor to work with the textarea. – epascarello Oct 24 '16 at 21:17
  • don't do that. do **NOT** dump arbitrary text from PHP into a javascript context. One single `'` in any of that text-from-php and you kill the entire JS script block with a syntax error. – Marc B Oct 24 '16 at 21:18
  • How then?I tried and this but notning var lio=""; CKEDITOR.instances['post_article'].setData(lio); When i put into setData('some text') then works.I can't pass the variable inside – liontass Oct 24 '16 at 21:25
  • Ideally you want to either get the data via ajax, thats what i have done recently, works a treat. Or if you do want to inject directly inline via php you would first want to encode it with a javascript equiv of encodeURIComponent http://stackoverflow.com/questions/1734250/what-is-the-equivalent-of-javascripts-encodeuricomponent-in-php and the from the javascript end you could use decodeURiComponent. Also you dont need to go via instances, replace will return an instance you can use. You also will want to wait for the instanceReady event before you call setData.. – Keith Oct 24 '16 at 21:33
  • A litlle more help about the second way?I'm newbie – liontass Oct 24 '16 at 22:10

1 Answers1

0

Finally i did it.The solution was to insert to textarea the code as seem below:

<textarea class="form-control" name="post_keimeno"  cols="30" rows="20"><?php echo htmlspecialchars($post_keimeno); ?></textarea>

The CKEDITOR takes only HTML text so the htmlspecialchars function encode the text from sql to the right format HTML text.

liontass
  • 730
  • 9
  • 24