Я создал несколько ckeditor в php, он может правильно отображать панель 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>
Вы должны получить сообщение об ошибке, потому что mysql_query
не существует.
Пожалуйста, используйте PDO
или mysqli_*
, а не mysql_*
. Он небезопасен и был полностью удален с PHP 7.
Этот 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('редактор2');
Чтобы сохранить несколько 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>
преобразовать <div contenteditable = "true"> в Textarea после того, как вы сможете сохранить в базе данных пример Programmingtechnologyworld.blogspot.com/2018/10/…