【问题标题】:Load html text from database to CKEDITOR for updating purpose将 html 文本从数据库加载到 CKEDITOR 以进行更新
【发布时间】:2015-11-01 18:57:56
【问题描述】:

我有一个 html 表单,我使用 CKEDITOR 代替 textarea 来进行格式化。提交表单后,将 html 表单中的数据插入 MySQL 数据库。在我尝试从数据库中检索数据之前,一切正常。

我正在尝试将数据从 MySQL 检索到 CKEDITOR,在那里我可以更新它们并将它们再次保存到数据库中。

<?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>

变量$data 等于数据库中的某个值。如果变量等于某个数字/字符串,则一切正常。但是数据库中的数据被检索为带有 HTML 标记的文本,CKEDITOR 可能无法读取。

是否可以从数据库中检索 html 文本并在 CKEDITOR 中显示为没有 html 标签的格式化文本?

【问题讨论】:

    标签: javascript php html mysql ckeditor


    【解决方案1】:

    $data 的具体外观如何?也许你必须使用htmlspecialchars_decode

    【讨论】:

    • 当我在 textarea 中使用 htmlspecialchars_decode 时,它​​可以工作。谢谢。
    【解决方案2】:

    首先,你必须用下面的命令替换字符串:

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

    然后你只需要把这个数据变量放在文本框标签之间。如下:

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

    您可以从以下链接中找到所有内容:

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

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多