trenccan
trenccan

Reputation: 720

Load html text from database to CKEDITOR for updating purpose

I have a html form and I'm using CKEDITOR as a replacement of textarea for formatting purposes. After submitting the form are data from html form inserted into MySQL database. Everything work's great until I tried to retrieve the data from database.

I'm trying to retrieve the data from MySQL to CKEDITOR where I could update them and save them again into to the database.

<?php
if(isset($_GET['id'])){  

$id=$_GET['id'];
$sql = "SELECT * FROM article WHERE id='$id'";
$result = $conn->query($sql);

// output data of each row
while($row = $result->fetch_assoc()) { 
$data=$row['topic'];
}
} 

?>

<textarea type="text" name="topic" id="topic" rows="10" cols="80">    </textarea>
<script>
            // Replace the <textarea id="topic"> with a CKEditor
            // instance, using default configuration.
            CKEDITOR.replace( 'topic' );

            CKEDITOR.instances.topic.setData( '<p><?php echo $data;?></p>',    function()
{
this.checkDirty();  // true
});
</script>

Variable $data equals some value from database. If the variable is equal some number / string, everything works. But the data from database are retrieved as text with HTML tags and the CKEDITOR can't read that probably.

Is it possible to retrieve html text from database and show it in the CKEDITOR as a formatted text without html tags?

Upvotes: 0

Views: 5831

Answers (2)

Nikhil Vishwakarma
Nikhil Vishwakarma

Reputation: 19

First of all, you have to replace the string with below command :

$data = str_replace( '&', '&amp;', $get_database_variable);

and then you just have to put this data variable in between textbox tags. as below:

<textarea ><?= $data ?></textarea>

All you can find from below link :

https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/saving-data.html

Upvotes: 0

Szymon D
Szymon D

Reputation: 441

How does exactly look $data? Maybe you have to use htmlspecialchars_decode.

Upvotes: 1

Related Questions