【问题标题】:how to save multiple ckeditor in php?如何在php中保存多个ckeditor?
【发布时间】:2019-05-28 11:48:59
【问题描述】:

我在php中创建了多个ckeditor,它可以正确显示ckeditor的面板,但是当我在其中添加内容后点击更新时,它没有保存到数据库中。

<?php
if(isset($_POST["action"])&&($_POST["action"]=="update")){  
$id = $_POST['id']; 

$content = $_POST['content'];

$query_update = "UPDATE `correspond` SET content='$content' WHERE id='$id'"; 

mysql_query($query_update);

header("Location: correspond.php");
}
?>

<table>
<?php while($row_correspond_result=mysql_fetch_assoc($correspond_result)){ ?>
<tr class="table table-bordered table-striped"><form action="" method="POST" name="correspond_form" id="correspond_formJoin">
<tbody id="myTable">
<td colspan="3" align="center"><div contenteditable="true" class="myContent" id="content"><?php echo $row_correspond_result["content"];?>        </div>
<script>
var elements = document.getElementsByClassName( 'myContent' );
for ( var i = 0; i < elements.length; ++i ) {
CKEDITOR.inline( elements[ i ], { /* config for this instance */ } );
}});
</script>
</td>
</tr><tr>
<td colspan="2"></td>
<td align="center">
<input name="action" type="hidden" id="action" value="update">
<input type="submit" name="Submit" value="Update"></td>

</form></tr><?php }?> </tbody></table>

【问题讨论】:

  • 转换为 Textarea 之后可以存储到数据库示例 programmingtechnologyworld.blogspot.com/2018/10/…
  • 你应该得到一个错误,因为mysql_query 不存在。
  • 请使用PDOmysqli_*,而不是mysql_*。它是不安全的,在 PHP 7 中被完全删除。

标签: javascript php mysql


【解决方案1】:

要保存多个ckeditor,您需要以数组格式传递名称属性。当你点击提交时,使用序列化函数保存内容值。

PHP 代码。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST["action"])&&($_POST["action"]=="update")){  
    $content = $id = '';
    $form_data = $_POST['form_data'];
    for($i=0 ; $i<count($form_data['id']); $i++){
        $content = serialize($form_data['content'][$i]);
        $id = $form_data['id'][$i];
        $query_update = "UPDATE correspond SET content='$content' WHERE id= $id"; 
        mysqli_query($conn, $query_update);
    }
    //header("Location: correspond.php");
}
?>

在显示时反序列化内容值并放入 textarea 字段。

HTML 代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdn.ckeditor.com/4.11.4/standard/ckeditor.js"></script>

<script type="text/javascript">
$(document).ready(function() {
var elements = document.getElementsByClassName( 'myContent' );
for ( var i = 0; i < elements.length; ++i ) {
     CKEDITOR.replace( elements[ i ]);
    //CKEDITOR.inline( elements[ i ], { /* config for this instance */ } );
}
});
</script>

<table>
<tr class="table table-bordered table-striped">
<form action="" method="POST" name="correspond_form" id="correspond_formJoin">
<tbody id="myTable">
<td colspan="3" align="center">

<?php 
$sql = "SELECT id, content FROM correspond";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
while($row = $result->fetch_assoc()) {
    //get the content form database and use unserialize method
    $content = unserialize($row["content"]);
?>

<!-- Change div to textarea and use the name attr as array format -->
<textarea contenteditable="true" class="myContent" name="form_data[content][]" id="content"><?php echo $content;?> </textarea>
<!-- add the corresponding id to update the content. -->
<input type="hidden" name = "form_data[id][]" value ="<?php echo $row["id"];?> ">
<?php
}
}
?>
</td>
</tr><tr>
<td colspan="2"></td>
<td align="center">
<input name="action" type="hidden" id="action" value="update">
<input type="submit" name="Submit" value="Update"></td>



</form></tr></tbody></table>

【讨论】:

    【解决方案2】:

    这个 div 转换为 TextArea

    来自

    <div contenteditable="true" class="myContent" id="content"><?php echo $row_correspond_result["content"];?>        </div>
    

    <textarea contenteditable="true" class="myContent" id="content"><?php echo $row_correspond_result["content"];?> </textarea>
    

    【讨论】:

    • 谢谢,但是当我更改为 textarea 时,ckeditor 面板不显示,有什么建议吗?
    • CKEDITOR.replace(elements[ i ], { /* 此实例的配置 */ } );例如,因为您使用多个编辑器 CKEDITOR.replace('editor1'); CKEDITOR.replace('editor2');
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签